Question:
In Access, I can get the date that was this day of this week a year ago (i.e., if today is Monday, 5/15/00, a year ago was Monday, 5/17/99) by using this function in VBA:
DateAdd("ww",-52,Date)
Is there a way to do this in SQL?
Answer:
This can be done in SQL using the DATEADD function also. You can use ww or wk to specify weeks. Here’s a SQL string that returns today’s date and this day of the week a year ago:
select datename(dw, getdate()) + " " + cast(getdate() as varchar) as'Today', datename(dw,dateadd(ww, -52, getdate())) + " " + cast(dateadd(ww, -52, getdate()) as varchar) as 'A Year Ago'
Assuming today is 5/18/00 returns (abridged format):
- Today
Thursday May 18 2000 3:53PM
A Year Ago
Thursday May 20 1999 3:53PM