I want to get records from previous year and future year by using current year. I tried but coudnt get proper query. thanks in advance if anyone help.
I've tried this query below:
SELECT COL1, COL2
FROM TABLE1
WHERE YEAR(PRODUCTEFFECTIVEDATE) = YEAR(CURDATE())
AND PRODUCTEFFECTIVEDATE> DATEADD(year, -1, GETDATE());
In general you can query year in so many ways. This is one:
#for previous year
SELECT *
FROM mytable
WHERE YEAR(date_column)=YEAR(CURDATE() - INTERVAL 1 YEAR);
#for future year
SELECT *
FROM mytable
WHERE YEAR(date_column)=YEAR(CURDATE() + INTERVAL 1 YEAR);
#for previous year and future year
SELECT *
FROM mytable
WHERE YEAR(date_column)=YEAR(CURDATE() - INTERVAL 1 YEAR)
AND YEAR(date_column)=YEAR(CURDATE() + INTERVAL 1 YEAR);
You only need to subtract(-) or add(+) interval of how many years you want.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.