Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: SQL Server
Expertise: Beginner
May 23, 2000

Year-Ago-Today Date Function

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
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date