简体   繁体   中英

SQL Query to Select * From Table where date is between 7 days ago and now

I am trying to write a simple query in SQL Server (2005 express). The purpose is to only choose data between now and 7 days ago. I cannot seem to get the GETDATE function to work in this example... Any ideas?

**PS - The date time in the column is in EPOCH so I believe the issue may be stemming from here with the datatype...

Select * From TB_Data
where TB_Data.nDate <= GETDATE()-7)

Adding a parenthesis before the GETDATE() function will work.

Select * From TB_Data
where TB_Data.nDate <= (GETDATE() - 7)

With EPOCH, you will need to convert the date before comparing:

DATEADD(s, TB_Data.nDate, '19700101')

So the full query is:

Select * From TB_Data
where DATEADD(s, TB_Data.nDate, '19700101') <= (GETDATE() - 7)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM