how to do sec_to_time in SQLite? - 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

Related

How do i subtract time from datetime in snowflake?

pdt.startTime is datetime
s_first.FromTimeOfDay is a time
I want to subtract the time drom the datetime. When i run the code below, Snowflake gives me this error invalid type [CAST(S_FIRST.FROMTIMEOFDAY AS TIMESTAMP_NTZ(9))] for parameter 'TO_TIMESTAMP_NTZ'
select (pdt.StartTime - (SELECT s_first.FromTimeOfDay::datetime FROM Shift s_first))
from RAW_CPMS_AAR.POWERBI_DowntimeTable AS PDT
When i try this:
select (pdt.StartTime::TIMESTAMP_NTZ(9) - (SELECT s_first.FromTimeOfDay::TIMESTAMP_NTZ(9) FROM Shift s_first))
from RAW_CPMS_AAR.POWERBI_DowntimeTable AS PDT
I get more or less the same error: invalid type [CAST(S_FIRST.FROMTIMEOFDAY AS TIMESTAMP_NTZ(9))] for parameter 'TO_TIMESTAMP_NTZ'
How do I convert the time into a datetime format so that I can subtract the two. It doesnt seem to me that there is a clear way to convert time into datetime in snowflake.
Is this what you're after?
select current_timestamp() as sample_timestamp
, time(sample_timestamp) as sample_time
, date(sample_timestamp) as sample_date;
A user pointed me in the right direction. i didnt realize i could use "dateadd" to also subtract time.
dateadd(HOUR, - (HOUR(current_timestamp())), temp.DateTime)

SQLite convert milliseconds to HH:MM:SS

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');

Sqlite - Calculation of time period

I need to calculate the time between a 'fixed' time (usually a few days ago) and now.
Therefore I have googled me to this sql (http://www.sqlite.org/lang_datefunc.html):
SELECT strftime ('%s', '1423143532') - strftime ('%s', 'now') as timediff;
It gives a weird result (122747310831610), and I have no idea why !? :-/
Should I use a different method of calculation, is that where the problem is? If yes, which one would give me the correct result? :)
A bare number is interpreted as a Julian day number:
> SELECT datetime('1423143532');
3891722-46318118-31 12:00:00
> SELECT datetime(2457064);
2015-02-10 12:00:00
To interpret a number as the number of seconds since 1970, you must use the unixepoch modifier:
> SELECT datetime(1423143532, 'unixepoch');
2015-02-05 13:38:52
> SELECT strftime ('%s', 1423143532, 'unixepoch');
1423143532
As you can see, passing this number through strftime is pointless. Just use it directly:
> SELECT 1423143532 - strftime('%s', 'now') as timediff;
-436916

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