16

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?

1 Answer 1

27

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);
Sign up to request clarification or add additional context in comments.

1 Comment

For mer worked: SELECT CAST(strftime('%s', strftime('%Y-%m-%dT00:00:00+00:00', <your_datetime_column>), 'unixepoch') as datetime) FROM <your_table>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.