Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

todays date for last year

Posted on 2009-12-17
11
Medium Priority
?
288 Views
Last Modified: 2012-05-08
today is Thursday 12-17-2009. Last year was Thursday 12-18-2009. so how do i get the date of last year correctly in tsql.
in vb its:Now.AddYears(-1).AddDays(Now.DayOfWeek - Now.AddYears(-1).DayOfWeek) so what is it in tsql? or how do i do it? thanks
0
Comment
Question by:StewSupport
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 26073551
DATEADD(year, -1, GETDATE())
0
 
LVL 35

Expert Comment

by:YZlat
ID: 26073563
SELECT DATEADD(year, -1, GETDATE())
0
 
LVL 35

Expert Comment

by:YZlat
ID: 26073613
SELECT DATEADD(day, DATEPART(dw,GETDATE()) - DATEPART(dw,DATEADD(year, -1, GETDATE())), DATEADD(year, -1, GETDATE()))
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:YZlat
ID: 26073623
the first two give you todays date, one year ago. The last one gives you exactly same thing as your vb code would return
0
 

Author Comment

by:StewSupport
ID: 26073624
no no. because the same day of today's date for last year is differnt. as you can see in my question today is thursday 12-17, but last year it was thursday 12-18. it might be 12-19 next year. i dont know. so is there a way to find out
0
 
LVL 29

Expert Comment

by:Kumaraswamy R
ID: 26073643
0
 
LVL 13

Expert Comment

by:sameer2010
ID: 26073662
Do you just want 1 year subtraction? Because the example you have posted shows "today is Thursday 12-17-2009. Last year was Thursday 12-18-2009" Not sure what is 12-18...
0
 
LVL 35

Accepted Solution

by:
YZlat earned 2000 total points
ID: 26073699
yes, I know it's different, it's 12/18/2008 and that's exactly what my sql above returns. I will type it again for you
SELECT DATEADD(day, DATEPART(dw,GETDATE()) - DATEPART(dw,DATEADD(year, -1, GETDATE())), DATEADD(year, -1, GETDATE()))

Open in new window

0
 

Author Comment

by:StewSupport
ID: 26073708
sorry there's been a mistyped in the qusetion it should be 12-18-2008 not 2009. Sorry about that. so if a user give a date for any year i'm suppose to find the date of last year. like today thursday 12-17-2009 the date for last year is 12-18-2008. thank you very much and sorry for the typo.
0
 
LVL 13

Expert Comment

by:sameer2010
ID: 26073730
YZIat cracked it!
0
 

Author Closing Comment

by:StewSupport
ID: 31667412
thank you
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to use Variables  and Custom code in SSRS report and Assembly reference to use compile shared code in SSRS. Its big question for all who are working with SSRS. It is easy to create assembly and refer in SSRS report, still there are some steps…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question