How to get last 3 months records from the table.
SELECT * from table where month > CURRENT_DATE-120 and month < CURRENT_DATE order by month;
I have used the above query is it correct? shall I use this for get last 3 month record from the table.
select (date_add(FROM_UNIXTIME(UNIX_TIMESTAMP(), 'yyyy-MM-dd'), 2 - month(FROM_UNIXTIME(UNIX_TIMESTAMP(), 'yyyy-MM-dd')) )); This results in 2015-05-30. The results should be like: if Today is '2015-06-03', then the result of last two months should be like: '2015-04-01'.
Instead of approximating the "current" date by selecting the MAX(date) the code could reference CAST(GETDATE() as DATE) to access the system datetime and cast it as type DATE. where [date] > dateadd(month, -6, cast(getdate() as date));
SELECT * FROM product WHERE pdate >= DATEADD(day, -30, getdate()).
You can use built-in INTERVAL
instruction
Check how this works:
SELECT CURRENT_DATE - INTERVAL '3 months'
and you can rewrite your SQL to:
SELECT * from table where date > CURRENT_DATE - INTERVAL '3 months'
(not checked but this should give you an idea how to use INTERVAL instruction)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With