Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a datetime string to a UNIX timestamp in SQLite3?

The documentation for SQLite3 datatypes says

the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values

I want to use those functions to store INTEGER values. How do I do so? The documentation for SQLite3 datetime functions describes the types of the arguments to those functions, but says nothing of the return type. It seems that the return type is text:

sqlite> select typeOf(datetime('2014-12-12 12:12:12.000'));
text

This is not what I want -- I want an integer value representing that time as a UNIX timestamp. Even when I create a column of type integer and attempt to store a datetime(...) value in it, SQLite stores it as a text value in that integer column.

How do I force the datetime function and friends to return a UNIX timestamp, instead of a text value?

like image 284
jameshfisher Avatar asked Dec 18 '14 11:12

jameshfisher


People also ask

Does an SQLite database support DateTime objects?

Date and Time Datatype. SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values: TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.

What is the date format in SQLite?

The date() function returns the date as text in this format: YYYY-MM-DD. The time() function returns the time as text in this format: HH:MM:SS. The datetime() function returns the date and time as text in their same formats: YYYY-MM-DD HH:MM:SS.


1 Answers

All the built-in date/time functions return strings.

To convert a string into a number, use CAST:

SELECT CAST(strftime('%s', 'now') AS INT);
like image 128
CL. Avatar answered Oct 11 '22 06:10

CL.