Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you find the same weekday last year in SQL?

Often in sales reports and so on you need to compare this day to the same day last year, but based on the same "weekday", not "day of month".

So for example, today is the 20th June 2013 and a Thursday. I want to see sales for today, versus the same THURSDAY last year (21st June 2012, as opposed to 20th June 2012 which was a Wednesday).

How can this be done in T-SQL?

like image 475
David Duffett Avatar asked Jun 20 '13 13:06

David Duffett


1 Answers

DECLARE @now Date
SET @now = '2013-06-20' -- your example
SELECT DATEADD(week, -52, @now)

SET @now = '2012-06-21' -- leap year test
SELECT DATEADD(week, -52, @now)
like image 99
gbn Avatar answered Sep 28 '22 06:09

gbn