Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query for today's date and 7 days before data?

Tags:

sql

I'm using sql server 2008. How to query out a data which is the date is today and 7 days before today ?

like image 902
Husna5207 Avatar asked Jul 18 '13 07:07

Husna5207


People also ask

How do I get last 7 days data in SQL query?

We use system function now() to get the latest datetime value, and INTERVAL clause to calculate a date 7 days in the past.

How do I add 7 days to a date in SQL?

SQL Server DATEADD() Function The DATEADD() function adds a time/date interval to a date and then returns the date.

How do I get today's date in a query?

SQL Server GETDATE() Function The GETDATE() function returns the current database system date and time, in a 'YYYY-MM-DD hh:mm:ss. mmm' format.


2 Answers

Try this way:

select * from tab
where DateCol between DateAdd(DD,-7,GETDATE() ) and GETDATE() 
like image 167
Robert Avatar answered Sep 21 '22 13:09

Robert


Query in Parado's answer is correct, if you want to use MySql too instead GETDATE() you must use (because you've tagged this question with Sql server and Mysql):

select * from tab
where DateCol between adddate(now(),-7) and now() 
like image 43
Joe Taras Avatar answered Sep 21 '22 13:09

Joe Taras