SQLite convert milliseconds to HH:MM:SS - sqlite

based on the SQLite documentation, there is time() that is equivalent with strftime('%H:%M:%S', ...). SO i tested it with my query
time(time)
but after run the query, all the result become 12:00:00
the data is basically in milliseconds (25000, 5000, 15000)
What is the problem??

The built-in date/time functions interpret numbers as Julian day numbers, unless you use the unixepoch modifier to specify seconds:
SELECT time(time / 1000, 'unixepoch');

Related

how to do sec_to_time in SQLite?

I have a field with seconds after midnight, and I have to convert it to HH:MM:SS. MySQL has sec_to_time(), which would be perfect, but SQLite does not.
How to convert?
I feel like it's some combination of strftime or date, and some dividing by 3600, but I can't get it.
For example, I need to convert 3601 to 1:01. Or, 32405 -> 9:05.
Use time()
select time(3601, 'unixepoch');
will give:
01:00:01

SQLite add milliseconds to time

I have a sqlite database where I want to add 84 seconds to each time filed. The time is formatted like this:
yyyy-MM-dd hh:mm:ss:zzz
2017-12-15 11:50:12.132
I've tried to modify the time with
UPDATE sensordata
SET time=DATETIME(time, '+84.000 seconds')
This adds 84 seconds correctly, but it deletes the milliseconds:
2017-12-15 11:51:36
How can I add the seconds and still have the milliseconds?
The datetime function does not format the fractional part.
You can use strftime() with the exact format you want :
(Edited to remove the redundant %S)
UPDATE sensordata
SET time=STRFTIME('%Y-%m-%d %H:%M:%f', time, '+84.000 seconds')
In fact datetime(...) is equivalent to strftime('%Y-%m-%d %H:%M:%S', ...), see Date And Time Functions for more details.

SQLite doesn't seem to convert unix epoch correctly

I insert timestamps in a column of type real. The timestamps are in unixepoch format, eg 1505720496876 which is GMT: Monday, September 18, 2017 7:41:36.876 AM.
However, when I perform the query
select datetime(timestamp, 'unixepoch', 'localtime') from history I get -1413-03-01 14:07:12 as result.
What am I doing wrong?
You are currently using milliseconds but you need to use seconds:
select datetime(timestamp / 1000, 'unixepoch', 'localtime')
from history
From the documentation:
The "unixepoch" modifier (11) only works if it immediately follows a timestring in the DDDDDDDDDD format. This modifier causes the DDDDDDDDDD to be interpreted not as a Julian day number as it normally would be, but as Unix Time - the number of seconds since 1970.

Storing my dates as long (milliseconds) in SQLite: Can I use strftime(...)?

I'm storing my dates in SQLite in a column of data type INTEGER. I'm storing the milliseconds since 1970.
Eg:
date (long) other columns ...
-----------------------------------------
1407297600000 ...
1407211200000 ...
1407124800000 ...
My question is: how can I use strftime() under this circumstances?
If not, I should use TEXT as the column type??
Running this:
select strftime('%Y-%m', date) from my_table;
Is throwing nonesense stuff:
strftime('%Y-%m', date)
-----------------------------------------
1968-19
1968-19
1968-19
Unless you tell it otherwise, strftime() thinks those numbers are Julian day values - very different from Unix epoch milliseconds.
You'll want to convert to seconds, and tell strftime() these are Unix epoch numbers:
select strftime('%Y-%m', date / 1000, 'unixepoch');
See the Modifiers section in the SQLite Date and Time Functions docs.

I need a sqlite equivalent of the folling msaccess query

Select distinct Format(DateAdd(""s""," & columnname & ",""1/1/1980 12:00:00 AM""), 'dd-MMM-yyyy') as A
I have assumed that the seconds to add and the original date are hard coded values below whilst awaiting clarifications requested in the comments.
To add a number of seconds to a date you can use:
select datetime('1980-01-01 00:00:00', "345000 seconds");
This gives the result: 1980-01-04 23:50:00
The example above is just under 4 days in seconds, if you want to truncate the result to just the date as implied by the query in your questions then you can wrap this inside a date function. However, this would give the result in the format "YYYY-MM-DD" rather than "DD-MMM-YYYY" as your access query does.
Unfortunately I cannot find any native SQLite function to convert a numeric month value to mmm format. You can do this manually with replace (similar to the answer to this question), but this is a bit messy.
If you are happy to live with the numeric months then you can simply use:
select strftime('%d-%m-%Y', '1980-01-01 00:00:00', "345000 seconds");
This gives the result: 04-01-1980
More information on the SQLite date / time functions can be found here.

Resources