nikdoof.com

/posts/ 2008/sql-server-last-full-week

SQL Server Last Full Week

Sep 17, 2008

#mssql

Warning: This post was written 5861 days ago, and may contain incorrect information, outdated opinions, and incorrect language due to my age at the time. Opinions in this post are not representative of who I am as a person today.

While writing a new report today I’ve been driven mad while creating a dynamic query that selects the last full week. So here is the code for SQL Server, to save anyone else the pain:

dateadd(dd,0, datediff(dd,0,
   dateadd(day,-1*datepart(weekday,getdate())+1,dateadd(week,-1,getdate()))
))
dateadd(dd,0, datediff(dd,0,
   dateadd(day,7,dateadd(day,-1*datepart(weekday,getdate()),dateadd(week,-1,getdate())))
))