Skip to content
Advertisement

SQLite – TimeStamp to Date

I am trying to convert a timestamp to date in SQLite.

But it give me always Null back, I try many solution I find out, but any solution works for me

Here is my request : Image

Thats my SQL script, if you want to try:

Advertisement

Answer

To expand on @forpas comment, SQLite does not have a TIMESTAMP data type, so when you insert values into your fromDateTime and toDateTime column they are converted to one of SQLite’s 5 data storage classes: NULL, INTEGER, REAL, TEXT, BLOB. Since there is no error on INSERT, this gives the impression that SQLite has recognised the value as a timestamp, when in fact the value has just been treated as TEXT. Now to use those values in any of SQLite’s Date and Time functions they must either be an ISO-8601 compatible string, the word now, or a number (interpreted as either a Julian day number or a Unix timestamp dependent on the context). So, you need to change your times to YYYY-MM-DD hh:mm:ss format i.e.

Note that datetime is simply called with the column as a parameter and returns the string in an ISO-8601 format. To get YYYY-MM-DD format you need to use strftime as well. So your query becomes:

And the output:

Demo on dbfiddle

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement