Home > Database > Mysql Tutorial > How Can I Perform Date Calculations Ignoring the Year?

How Can I Perform Date Calculations Ignoring the Year?

Barbara Streisand
Release: 2024-12-31 22:17:10
Original
284 people have browsed it

How Can I Perform Date Calculations Ignoring the Year?

How to Perform Date Calculations Ignoring the Year

To select dates that have an anniversary within the next 14 days, excluding the year, consider using the following approach:

SELECT *
FROM event
WHERE f_mmdd(event_date) BETWEEN f_mmdd(current_date)
                            AND     f_mmdd(current_date + 14);
Copy after login

This query uses the f_mmdd() function to calculate an integer value from the MMDD pattern of the date. The resulting values are used to compare with the current date and a date 14 days in the future.

The f_mmdd() function is defined as follows:

CREATE OR REPLACE FUNCTION f_mmdd(date)
  RETURNS int LANGUAGE sql IMMUTABLE PARALLEL SAFE STRICT AS
'SELECT EXTRACT(month FROM )::int * 100 + EXTRACT(day FROM )::int';
Copy after login

This function allows for the creation of a multicolumn expression index on the event_mmdd_event_date_idx column, optimizing query performance.

The above is the detailed content of How Can I Perform Date Calculations Ignoring the Year?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template