How can you get today's date and convert it to 01/mm /yyyy
format and get data from the table with delivery month 3 months ago? Table already contains delivery month as 01/mm/yyyy
.
4 Answers
SELECT *
FROM TABLE_NAME
WHERE Date_Column >= DATEADD(MONTH, -3, GETDATE())
Mureinik's suggested method will return the same results, but doing it this way your query can benefit from any indexes on Date_Column
.
or you can check against last 90 days.
SELECT *
FROM TABLE_NAME
WHERE Date_Column >= DATEADD(DAY, -90, GETDATE())
-
6Actually you can do
GETDATE()-90
insteadDATEADD(DAY, -90, GETDATE())
Feb 20, 2014 at 16:45 -
8@huMptyduMpty But 3 months is not necessarily 90 days, because months may have 30 or 31 days (or even 28 or 29 if we take February into account)– AlexBMay 2, 2017 at 12:22
Latest Versions of mysql don't support DATEADD instead use the syntax
DATE_ADD(date,INTERVAL expr type)
To get the last 3 months data use,
DATE_ADD(NOW(),INTERVAL -90 DAY)
DATE_ADD(NOW(), INTERVAL -3 MONTH)
-
4
I'd use datediff
, and not care about format conversions:
SELECT *
FROM mytable
WHERE DATEDIFF(MONTH, my_date_column, GETDATE()) <= 3
Last 3 months
SELECT DATEADD(dd,DATEDIFF(dd,0,DATEADD(mm,-3,GETDATE())),0)
Today
SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0)
WHERE Convert(DATE,TableColumn)>= GETDATE()-90
?? Did you try anything so far?