Crypcode
Crypcode

Reputation: 188

SQL where DATEDIFF

Hi guys i have table in mysql

enter image description here

I need filter results where created_at and updated at difference between those timestamps 2 hours or less

I'am trying to use:

SELECT * 
FROM `imoniu_r_padaliniai`
WHERE DATEDIFF(hour, created_at, updated_at) <= 2;

But i get syntax errot how achieve this?

Upvotes: 0

Views: 190

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270181

Just use direct comparisons:

where updated_at <= created_at + interval 2 hour

In MySQL and MariaDB, datediff() only handles date differences. If you want arbitrary time differences, you need timestampdiff(). However, I recommend using direct comparisons instead.

The three-argument form of datediff() is the syntax in SQL Server (and a handful of other databases), not MySQL.

Upvotes: 1

Related Questions