convert week datepart number to date within week (week of...)

wondergoat77 picture wondergoat77 · Sep 13, 2012 · Viewed 10k times · Source

I am trying to figure out how to convert a datepart for a week to the Monday of that week.

I am using this:

     datepart(ww, DateCompleted) as week 

to group a set of orders in our system by week. My goal is for this to say instead of 37, i want it to return the Monday of week 37, i.e.: 09-10-2012. I am able to do this by defining each week per number but i would rather not spend so much time typing out each week when there is probably a better option out there, i have just been unable to find it anywhere online other than in MySQL and other programming languages (i need T-SQL please, or anything that will work on sql server 2005), and i do not know how to work with that. Thanks for anyone that can help. Ill be checking back as often as possible to provide more info if needed.

Answer

Brad Christie picture Brad Christie · Sep 13, 2012

You can combine DATEADD and DATEPART to get the monday of the date supplied:

DATEADD(day, (-1 * DATEPART(dw, datecompleted)) + 2, datecompleted)

Example found here