Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 retrieve last week rows

Author  Topic 

olibara
Yak Posting Veteran

94 Posts

Posted - 2010-11-03 : 08:49:58
Hello

Is there any "elegant" way to select all rows where date is during the previous of current week ?

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-11-03 : 09:05:17
where datecol>=dateadd(week,-1,getdate())

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2010-11-03 : 09:46:11
Or this will set a range from Mon 12:00 am of the previous week through that Sunday midnight.

where datecol >= dateadd(week, datediff(week, 0, getdate()), -7)
and datecol < dateadd(week, datediff(week, 0, getdate()), 0)


Be One with the Optimizer
TG
Go to Top of Page

olibara
Yak Posting Veteran

94 Posts

Posted - 2010-11-03 : 09:57:49
Thank you madhivanan and TG

I probably can improve TG suggestions with a between but I can't really figure out what can be the difference bewtween TG and madhivanan suggestions ?
Can both handle correctly the week before the first week of the year ?

Go to Top of Page

olibara
Yak Posting Veteran

94 Posts

Posted - 2010-11-03 : 11:14:06
Hello

Sorry I was probably not well awake !
TG' solution is the good one of course ;-)

Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2010-11-03 : 13:42:27
Glad you're awake and it's working for you

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -