Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server select where datetimeoffset older than 1 hour

Tags:

sql

sql-server

I know this should be easy and I do it no probelms in mySQL and Postgresql but I'm struggling with SQL Server. I want to select rows with a datetimeoffset field that's over an hour old.

select * from table where mydatetime < getdate() - 1 hour

I've tried dateadd and datediff but can't get it right.

like image 525
sipsorcery Avatar asked Jan 13 '10 07:01

sipsorcery


People also ask

Can we use datediff in where clause?

Note: DATEADD and DATEDIFF SQL function can be used in the SELECT, WHERE, HAVING, GROUP BY and ORDER BY clauses.

Should I use DATETIME2 or Datetimeoffset?

This depends on whether or not you need to include a time zone offset. If you need to include a time zone offset, then you'll need to use datetimeoffset. If not, then use datetime2, as you'll save storage space and eliminate any potential issues with having a (potentially wrong) time zone offset in your data.

How do I get last 10 minutes in SQL?

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

What is time offset in SQL?

A time zone offset specifies the zone offset from UTC for a time or datetime value. The time zone offset can be represented as [+|-] hh:mm: hh is two digits that range from 00 to 14 and represent the number of hours in the time zone offset.


1 Answers

WHERE mydatetime < DATEADD(hour, -1, SYSDATETIMEOFFSET())

For more see: DATEADD (Transact-SQL)

like image 175
Josh Avatar answered Sep 22 '22 09:09

Josh