Showing posts with label explain. Show all posts
Showing posts with label explain. Show all posts

Friday, February 17, 2012

Date Breakdown Question

Can anyone please explain how this line of SQL will give me the first day of the week of the date passed in. With 7 being the parameter at the back end dateadd parameter that will return sunday as the first day of the week but I don't get the datediff portion.

Select dateadd(wk, datediff(wk, 6, '04/09/2007'), 7)

I will separate the operation into two separate components.

First, this portion calculates the number of full weeks between day 6 of the first week and today. (For 2007/04/30, that is 5599.) It is necessary to remember that day 0 is actually the first day, day 1 is the second day, etc.


SELECT datediff(wk, 6, getdate())

--
5599

Then that value is used to find the first date of the week if you added 5599 weeks to Day 6 of the first week.


SELECT dateadd(wk, 5599, 6)

2007-04-29 00:00:00.000

|||

Thanks for replying

I thought that is what was happening but the datediff gave me a hard time.

Thanks again