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
 General SQL Server Forums
 New to SQL Server Programming
 Date Ranges

Author  Topic 

learzbu
Starting Member

27 Posts

Posted - 2011-11-17 : 12:04:05
Hi,

I wanted to know if there is a way to declare a non definite date range for running a query. What I want to do is run it for the previous week, so no matter what day I run the query on it will always run for the previous sunday to sunday period.

Is there anything like this possible?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-17 : 12:08:07
yep. its possible

SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE())/7 * 7,0)-8 AS Start,DATEADD(dd,DATEDIFF(dd,0,GETDATE())/7 * 7,0)-1 AS end


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

learzbu
Starting Member

27 Posts

Posted - 2011-11-17 : 13:36:32
Thanks Again!!
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-11-17 : 13:43:52
Doesn't like end



SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE())/7 * 7,0)-8 AS [Start],DATEADD(dd,DATEDIFF(dd,0,GETDATE())/7 * 7,0)-1 AS [end]
SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE()+1)/7 * 7,0)-8 AS [Start],DATEADD(dd,DATEDIFF(dd,0,GETDATE()+1)/7 * 7,0)-1 AS [end]




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

learzbu
Starting Member

27 Posts

Posted - 2011-11-17 : 14:44:35
Ya i just took out end and replaced with finish
Go to Top of Page
   

- Advertisement -