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
 Current date + last 6 days

Author  Topic 

hameedf
Starting Member

27 Posts

Posted - 2012-08-06 : 05:06:48
Hello expert !

I want add previous 6 days in current date. If i run a query it get the data of current date + previous 6 days?

----Last Day of Last Week
SELECT DATEADD(wk,DATEDIFF(wk,7,GETDATE()),6)
----Last Day of Current Week
SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),6)


Thanks
hf

Hameed

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2012-08-06 : 05:08:30
SELECT * FROM dbo.Table1 WHERE Col1 >= DATEADD(DAY, -6, GETDATE())



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

hameedf
Starting Member

27 Posts

Posted - 2012-08-06 : 05:19:32
Swepeso thank for your reply how to set in below set?

Set @StartDate = Case When @StartDate = '1900-01-01' Then DateAdd(dd, DateDiff(dd,0,GetDate()-7), 0) Else @StartDate End;

Thanks
hf

Hameed
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2012-08-06 : 08:11:26
[code]DECLARE @StartDate DATETIME

SET @StartDate = CASE @StartDate
WHEN '19000101' THEN DATEDIFF(DAY, '19000108', GETDATE()
ELSE @StartDate
END;[/code]


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-06 : 09:46:07
quote:
Originally posted by hameedf

Swepeso thank for your reply how to set in below set?

Set @StartDate = Case When @StartDate = '1900-01-01' Then DateAdd(dd, DateDiff(dd,0,GetDate()-7), 0) Else @StartDate End;

Thanks
hf

Hameed


to understand how this works see below

http://visakhm.blogspot.com/2012/07/generate-datetime-values-from-integers.html

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

Go to Top of Page
   

- Advertisement -