Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a SQL Server select using a timestamp minus a certain number of hours

Right now I have a SQL query that allows me to select entries in a table that have been inserted over the past day.

The query is:

Select account from mytable where create_date > current_timestamp - 1   

But suppose that I wanted to select entries that had been inserted over the base two hours?

How would I write that query?

like image 555
Elliott Avatar asked Jan 03 '11 15:01

Elliott


Video Answer


2 Answers

You can use DATEADD, like so:

SELECT account
FROM MyTable
WHERE create_date > DATEADD(hh, -2, GETDATE())
like image 191
AdaTheDev Avatar answered Oct 23 '22 08:10

AdaTheDev


Consider that a timestamp and a datetime are extremely different in sql server. A timestamp is a unique binary number whereas a datetime is a date and time combination. I think you'll need datetime.

like image 31
brian Avatar answered Oct 23 '22 10:10

brian