, " " , , - , , 1972-01-03.
, , .
In SQL Serveryou need to generate a list of years and join it:
WITH q AS
(
SELECT 0 AS num
UNION ALL
SELECT num + 1
FROM q
WHERE num <= 100
)
SELECT *
FROM mytable
WHERE fund_date BETWEEN DATE_ADD(year, -num, GETDATE()) AND DATE_ADD(year, -num, DATE_ADD(month, 2, GETDATE()))
UNION ALL
SELECT *
FROM mytable
WHERE fund_date <= DATEADD(year, -100, GETDATE()
AND DATE_ADD(year, YEAR(GETDATE()) - YEAR(fund_date), fund_date) BETWEEN GETDATE() AND DATE_ADD(month, 2, GETDATE()))
This request consists of two parts:
- The first part selects birthdays for each year from the list by scanning the index range.
- The second part selects birthdays that are more than
100years old (using a single-range scan) and filters them all.
Since birth dates older than 100years are very different, the second part actually selects almost nothing and does not affect performance too much.
source
share