devxlogo

Year-Ago-Today Date Function

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
See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist