November 16, 2008 at 6:59 pm
Hello:
I need help getting the dates for sunday and saturday of last week programatically.
I need to do a daterange off this, every new week should show the dates of the last week.
Thanks
November 16, 2008 at 8:45 pm
Use dateadd and datepart.
November 16, 2008 at 9:00 pm
hi,
One way to get those dates is:
select dateadd(dd,-(6+datepart(dw, getdate())),getdate())
select dateadd(dd,-datepart(dw, getdate()),getdate())
Of course that will give you the time as well which could cause problems. To format it differently you could either chop up the different parts and glue them back together or do something like...
select DATEADD(DAY,DATEDIFF(DAY,'20000101',GETDATE()-(6+datepart(dw, getdate()))),'20000101')
select DATEADD(DAY,DATEDIFF(DAY,'20000101',GETDATE()-datepart(dw, getdate())),'20000101')
This answer assumes that Sunday is the first day of your week and Saturday the last day. You can adjust it if that is not the case.
B
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply