How to create a query to extra current day and the 7 previous days

How to create a query to extra current day and the 7 previous days

Post by SnVhb » Fri, 20 Oct 2006 03:34:02


I need to extra data using a query in a weekly basis, How can I setup my
script where it will show the current day and the 5 previous days without
imputting a date range in the script.
 
 
 

How to create a query to extra current day and the 7 previous days

Post by Tracy McKi » Fri, 20 Oct 2006 03:47:54


Look up DATEADD or DATEDIFF in Books Online...

One method (assuming col4 is a DATETIME column) would be:

SELECT col1, col2, col3
FROM myTable
WHERE col4 BETWEEN DATEADD(dd, -5, GETDATE()) AND GETDATE()



--
Tracy McKibben
MCDBA
http://www.yqcomputer.com/

 
 
 

How to create a query to extra current day and the 7 previous days

Post by Aaron Bert » Fri, 20 Oct 2006 03:52:25

DECLARE @dt SMALLDATETIME;
SET @dt = DATEDIFF(DAY, 5, CURRENT_TIMESTAMP);
SELECT @dt;

--SELECT column_names FROM table_name WHERE datetime_column >= @dt;