Houman
Houman

Reputation: 66390

How to get last year's date range?

I would like to get a date range between LastYear/1/1 until LastYear/12/31

I know I could do this

date_sub(now(), interval 1 year). But this would get me 2013/03/08. Not sure how to change the day and the month.

SELECT *
FROM orders
WHERE dispatch_date between  `LastYear/1/1` AND `LastYear/12/31`

Upvotes: 0

Views: 596

Answers (2)

Pho3nixHun
Pho3nixHun

Reputation: 828

I would suggest you to use YEAR().

SET @LastYear = YEAR(DATE_SUB(NOW(),INTERVAL 1 YEAR));
SELECT *
FROM orders
WHERE dispatch_date 
    BETWEEN CONCAT(@LastYear,'-01-01') AND CONCAT(@LastYear,'-12-31')

Upvotes: 0

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33391

You can easy to create the required dates:

SELECT *
FROM orders
WHERE dispatch_date >= MAKEDATE(YEAR(NOW()) - 1, 1) -- first day of previous year
 AND dispatch_date < MAKEDATE(YEAR(NOW()), 1)  -- first day of current year

Upvotes: 1

Related Questions