Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert timestamp to string in SQLite?

I have table with timestamps in ms stored in it. I want to convert those timestamps in a human readable form.

Here is a sample output of my table:

SELECT date raw, strftime('%d-%m-%Y', (date/1000)) as_string
  FROM my_table

 +-----------------+--------------+
 |     raw         |  as_string   |
 +-----------------+--------------+
 |  1444687200000  |  06-47-3950  |
 +-----------------+--------------+
...               ...            ...
 +-----------------+--------------+

As you can see, the date as string is quite strange (06-47-3950).

How can I obtain 12-10-2015?

like image 438
Stephan Avatar asked Oct 13 '15 23:10

Stephan


People also ask

How do I change the date format in SQLite?

Use the STRFTIME() function to format date\time\datetime data in SQLite. This function takes two arguments. The first argument is a format string containing the date/time part pattern. In our example, we use the format string '%d/%m/%Y, %H:%M'.

Does SQLite have timestamp?

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 datetime format in SQLite?

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


2 Answers

Try this:

SELECT date raw, strftime('%d-%m-%Y', datetime(date/1000, 'unixepoch')) as_string
  FROM my_table

You need to convert timestamp to date before.

like image 183
Cristian Oliveira Avatar answered Sep 28 '22 05:09

Cristian Oliveira


You need to convert timestamp to daytime first. There was an answer on one forum. I quote it here.

Here you are: try those queries to see why and how.

select julianday('1899-12-30 00:00:00');

-- that gives 2415018.5 (remember Julian dates start at noon)

select datetime('40660.9454658044', '+2415018 days', '+12 hours', 'localtime');

-- gets you 2011-04-28 00:41:28 (depending on your local time)

like image 21
asd-tm Avatar answered Sep 28 '22 06:09

asd-tm