tijagi
tijagi

Reputation: 1244

How to compare dates in sqlite3?

There is a table with fields containing unix timestamps, and I was trying to get records that are older than one hour with

select * from table_name where (date('now','unixepoch') - table_field) > 3600

But this doesn’t show any records at all, but there are fields containing stamps of the time of days ago. If there is some simple condition like table_field > 1 it does output all the records. But why the statement at the top doesn’t work?

Upvotes: 1

Views: 167

Answers (1)

CL.
CL.

Reputation: 180070

The 'unixepoch' modifier converts from a Unix timestamp number to a format that the date function understands. However, you want to convert from a date string to a Unix timestamp:

SELECT *
FROM table_name
WHERE strftime('%s', 'now') - field > 3600

Instead of counting seconds, you could also use a modifier (but without the subtraction, the string returned by strftime is not automatically converted into a number):

SELECT *
FROM table_name
WHERE CAST(strftime('%s', 'now', '-1 hours') AS INT) > field

Upvotes: 1

Related Questions