I'm using sql server 2008. How to query out a data which is the date is today and 7 days before today ?
We use system function now() to get the latest datetime value, and INTERVAL clause to calculate a date 7 days in the past.
SQL Server DATEADD() Function The DATEADD() function adds a time/date interval to a date and then returns the date.
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.
Try this way:
select * from tab
where DateCol between DateAdd(DD,-7,GETDATE() ) and GETDATE()
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With