I have an Sqlite database in which I want to select rows of which the value in a TIMESTAMP column is before a certain date. I would think this to be simple but I can’t get it done. I have tried this:
SELECT * FROM logged_event WHERE logged_event.CREATED_AT < '2010-05-28 16:20:55'
and various variations on it, like with the date functions. I’ve read http://sqlite.org/lang_datefunc.html and http://www.sqlite.org/datatypes.html and I would expect that the column would be a numeric type, and that the comparison would be done on the unix timestamp value. Apparantly not. Anyone who can help? If it matters, I’m trying this out in Sqlite Expert Personal.
Edit:
Here’s type table description:
CREATE TABLE [logged_event]
(
[id] INTEGER NOT NULL PRIMARY KEY,
[created_at] TIMESTAMP,
[name] VARCHAR(64),
[data] VARCHAR(512)
);
And the test data:
INSERT INTO table VALUES(1,'2010-05-28T15:36:56+0200','test','test');
INSERT INTO table VALUES(2,'2010-05-28T16:20:49+0200','test','test');
INSERT INTO table VALUES(3,'2010-05-28T16:20:51+0200','test','test');
INSERT INTO table VALUES(4,'2010-05-28T16:20:52+0200','test','test');
INSERT INTO table VALUES(5,'2010-05-28T16:20:53+0200','test','test');
INSERT INTO table VALUES(6,'2010-05-28T16:20:55+0200','test','test');
INSERT INTO table VALUES(7,'2010-05-28T16:20:57+0200','test','test');
The issue is with the way you’ve inserted data into your table: the
+0200syntax doesn’t match any of SQLite’s time formats:Changing it to use the SS.SSS format works correctly:
If you absolutely can’t change the format when it is inserted, you might have to fall back to doing something “clever” and modifying the actual string (i.e. to replace the
+with a., etc.).(original answer)
You haven’t described what kind of data is contained in your
CREATED_ATcolumn. If it indeed a datetime, it will compare correctly against a string:If it is stored as a unix timestamp, you need to call
DATETIMEfunction with the second argument as'unixepoch'to compare against a string:If neither of those solve your problem (and even if they do!) you should always post some data so that other people can reproduce your problem. You should even feel free to come up with a subset of your original data that still reproduces the problem.