0

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)
ubuntuuber
  • 740
  • 1
  • 7
  • 18

1 Answers1

3

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)
Appel21
  • 226
  • 1
  • 7
  • I should also mention that the date time in the table is in EPOCH... My error now is failed when converting varchar value 'GetDATE() -7 to data type Int – ubuntuuber Apr 30 '14 at 15:49
  • 1
    Could you do something like: where DATEADD(s, TB_Data.nDate, '19700101') <= (GETDATE() - 7) Similar to: http://stackoverflow.com/questions/4787827/converting-epoch-timestamp-to-sql-serverhuman-readable-format – Appel21 Apr 30 '14 at 15:57