tags:

views:

162

answers:

1

Hi,

I am currently getting first day Of this week and last week values with vbscript function in 2/12/2009 format. I was wondering if it was possible with SQL query.

+4  A: 

These statements should do what you want in TSQL. Note, the statements are based on the current date. You can replace getdate() for whatever date you wish:

Select dateadd(wk, datediff(wk, 0, getdate()) - 1, 0) as LastWeekStart
Select dateadd(wk, datediff(wk, 0, getdate()), 0) as ThisWeekStart
Select dateadd(wk, datediff(wk, 0, getdate()) + 1, 0) as NextWeekStart

There are lots of other date routines here.

Ben Griswold
Nice Answer, Ben. Readers should note though that this will return the first day of these weeks as defined by SET DATEFIRST (and as seen with SELECT @@DATEFIRST), which may or may not be the same as what the reader expects.
RBarryYoung
@RBarry Young - Very good point. Thanks for calling this out.
Ben Griswold