SQLite: express the difference as days, hours, minutes between two given dates - sqlite

I am trying to express the difference of two given dates in days, hours, and minutes (like 1 day, 6 hours, 17 minutes.) as SQLite query output. I have entryin and entryout as datetime fields in a SQLitedatabase. I tried all combinations of julianday and strftime but still running into rough weather.
I tried strftime('%d %H:%M', julianday(entryout)-julianday(entryin)). For a row the values are 2011-11-10 11:46, and 2011-11-09 09:00. but the output is 25 14:46 instead of 01 02:46.
Can some one help me with this, or point me correct logic for this? Thanks in advance.

You can try something like this:
SELECT
CAST((strftime('%s', '2011-11-10 11:46') - strftime('%s', '2011-11-09 09:00')) / (60 * 60 * 24) AS TEXT) || ' ' ||
CAST(((strftime('%s', '2011-11-10 11:46') - strftime('%s', '2011-11-09 09:00')) % (60 * 60 * 24)) / (60 * 60) AS TEXT) || ':' ||
CAST((((strftime('%s', '2011-11-10 11:46') - strftime('%s', '2011-11-09 09:00')) % (60 * 60 * 24)) % (60 * 60)) / 60 AS TEXT);

Related

Show time diff as HH:MM:SS between two datetimes in SQLite

I got two different datetimes: 2020-05-18 12:30:01 and 2020-05-17 13:00:00.
I want to show the time difference between them in the format HH:MM:SS, which is 23:30:01.
If the difference is higher than 24 hours, let's say 28 hours, 12 minutes and 45 seconds, it would show like 28:12:45.
How can I do that in SQLite?
SQLite supports a limited number of functions for datetime manipulation.
One of these functions is strftime(), and
strftime('%s', somedate)
returns the number of seconds from '1970-01-01' up to somedate.
With the use of this function, arithmetic calculations, string padding and concatenations you can get what you want like this:
CASE WHEN ((strftime('%s', date1) - strftime('%s', date2)) / 3600) < 10 THEN '0' ELSE '' END ||
((strftime('%s', date1) - strftime('%s', date2)) / 3600) || ':' ||
SUBSTR('0' || (((strftime('%s', date1) - strftime('%s', date2)) / 60) % 60), -2) || ':' ||
SUBSTR('0' || ((strftime('%s', date1) - strftime('%s', date2)) % 60), -2)
Replace date1 and date2 with your dates.
See a simplified demo.
SELECT time(
(
julianday('2020-05-18 12:30:01')-
julianday('2020-05-17 13:00:00')
)*60*60*24, 'unixepoch'
);
answsers the question when time difference is lower than 24h...

plsql date to number of milliseconds

Is it possible in PLSQL convert date to number of milliseconds?
I tried
select to_number(TO_CHAR(sysdate, 'yyyymmddhh24miss')) * (24 * 60 * 60 * 1000) from dual;
but I get back 1743604888943174400000
which is not the same as java GetTime method
https://www.w3resource.com/java-tutorial/util/date/java_date_gettime.php
As swmcdonnell already said, your question is kind of dublicated from:
oracle systimestamp (sysdate) to milliseconds
But you got an understanding problem. I did split your query into three columns.
SELECT TO_CHAR (SYSDATE, 'yyyymmddhh24miss'), -- Here we convert our sysdate into a 'string'
TO_NUMBER (TO_CHAR (SYSDATE, 'yyyymmddhh24miss')), -- this will output the resulting string as number - it will look the same as the 1st column
TO_NUMBER (TO_CHAR (SYSDATE, 'yyyymmddhh24miss')) * (24 * 60 * 60 * 1000) -- here you calculated 20180613150101 * 24 * 60... i think that's not what you want to do
FROM DUAL;
This doesn't make sense.
If you want the 'total-milliseconds' you have to:
use current_timestamp instead of sysdate
multiply the year (yyyy) by 365 days * 24 hours * 60 minutes * 60 seconds * 1000 ms
multiply the day-of-year (DDD) by 24 hours * 60 minutes * 60 seconds * 1000 ms
multiply the hour * 60 minutes * 60 seconds * 1000 ms
multiply the minute * 60 seconds * 1000 ms
multiply the second * 1000 ms
add ms
This would result in something like this:
SELECT to_number(TO_CHAR (CURRENT_TIMESTAMP, 'yyyy')) * 365 * 24 * 60 * 60 * 1000
+ to_number(TO_CHAR (CURRENT_TIMESTAMP, 'DDD')) * 24 * 60 * 60 * 1000
+ to_number(TO_CHAR (CURRENT_TIMESTAMP, 'HH24')) * 60 * 60 * 1000
+ to_number(TO_CHAR (CURRENT_TIMESTAMP, 'mi')) * 60 * 1000
+ to_number(TO_CHAR (CURRENT_TIMESTAMP, 'ss')) * 1000
+ to_number(TO_CHAR (CURRENT_TIMESTAMP, 'FF'))
FROM DUAL;
.. But what are u going to do with it? I don't know Java but a Timestamp should not be displayed as interval. This would be something like interval in oracle or a timespan in C#.
SELECT TO_NUMBER(TO_CHAR(SYSDATE,'YYYYMMDDHHMISS'))||to_number(SUBSTR(TO_CHAR (CURRENT_TIMESTAMP, 'FF'),0,2)) FROM dual;

get the date difference in milliseconds in oracle

I need to get he difference between end date and start date in milliseconds inside a view in oracle 11g. I can get these two dates from the database in 07-JUN-12 04.32.21.092000000 AM format. All I need is to find the diff of these kind of dates in milliseconds
SELECT ((extract(DAY FROM time2-time1)*24*60*60)+
(extract(HOUR FROM time2-time1)*60*60)+
(extract(MINUTE FROM time2-time1)*60)+
extract(SECOND FROM time2-time1)) *1000
as millisecs FROM dual;
can be done using above approach
select (DATE1 - DATE2) as days,
(DATE1 - DATE2) * 24 as hours,
(DATE1 - DATE2) * 24 * 60 as minutes,
(DATE1 - DATE2) * 24 * 60 * 60 as seconds,
(DATE1 - DATE2) * 24 * 60 * 60 * 1000 as milliseconds
from dual
EDIT - I assumed DateTime type. However, Justin Cave's question is very relevant. A Timestamp is not the same as a DateTime, so my answer won't work if you are dealing with Timestamps.
In that case, see this http://www.dba-oracle.com/t_timestamp_math_elapsed_times.htm.

SQL Server 2008 convert int to dd:hh:mm

How can I convert an int value (ie: 1800) which represents minutes into a value that looks like this: dd:hh:mm (days:hours:minutes).
So 1800 should be converted into 1:06:00 (1 day 6 hours 0 minutes).
In a stored procedure I have this:
SELECT
Record_ID, Project_ID, Ticket_ID, WO_Type, DC, Title, Device_Quantity,
Total/1440 as Total,
((Total - Elapsed) - DATEDIFF(mi, Record_Time, getdate())) as FinalTimeLeft,
Completed
FROM Record
How would I implement the casting into the SP? Above FinalTimeLeft=1800
How about this example - it does hours, minutes and seconds but it should be easy to modify for days, hours and minutes:
SELECT
CAST(mins / 3600 AS VARCHAR) + ':' +
RIGHT('0' + CAST((mins % 3600) / 60 AS VARCHAR), 2) + ':' +
RIGHT('0' + CAST(mins % 60 AS VARCHAR), 2)
FROM
(SELECT 1800 AS mins) a
EDIT: Included your stored procedure with my code amended for day, hour and minute:
SELECT
*
,CAST(FinalTimeLeft / 1440 AS VARCHAR) + ':' +
RIGHT('0' + CAST((FinalTimeLeft / 60) % 24 AS VARCHAR), 2) + ':' +
RIGHT('0' + CAST(FinalTimeLeft % 60 AS VARCHAR), 2) AS duration
FROM (
SELECT
Record_ID
,Project_ID
,Ticket_ID
,WO_Type
,DC
,Title
,Device_Quantity
,Total/1440 as Total
,((Total-Elapsed)-DATEDIFF(mi,Record_Time,getdate())) as FinalTimeLeft
,Completed
FROM record) a

sqlite - making TIMEDIFF as in MySQL

I want to make a function call that hase efect in SQLite like TIMEDIFF in MySQL.
I made this:
select strftime('%s','2012-01-01 12:00:00') - strftime('%s','2004-01-01 02:34:56')
but this is just the number of seconds. So how can i make a str like %Y-%m-%d %H:%M:%S where %H:%M:%S is the hours, minutes and seconds difference, and when it is bigger then 24 hours then %d will show how much dais it is and so on with %Y and %m.
You cannot represent a time difference with %Y-%m-%d ..., at least not as a date format. How would you express less than a day of difference? (0000-00-00 ... is not a valid date). Also, what would a month be? 30 days? 31 days? 23423432 days?
I suggest you keep your difference in seconds, and when presenting it you adapt it as necessary.
On the other hand, if you really want to do as you asked, here's one way:
sqlite> select datetime(strftime('%s','2012-01-01 12:00:00')
- strftime('%s','2004-01-01 02:34:56') - 62167305600, 'unixepoch');
0007-12-31 09:25:04
Even if I feel the downvote by the OP wasn't justified, I can't stop myself from explaining why what I mentioned above as clearly not a very good option returns "incorrect" results when the time difference is less than 1 day: the reason is implied in what I wrote above: there is no such date as 0000-00-00 ... so instead the datetime returned goes in negative territory: -001-12-31 ...
Here's a way to obtain 438:53:45, but it's quite involved:
earlier date: d1
later date: d2
select
cast(
(strftime('%s', d2) - strftime('%s', d1)) / 86400 * 24
+ cast(strftime("%H", time(strftime('%s', d2)
- strftime('%s', d1), 'unixepoch'))
as int)
as text)
|| ":"
|| substr(time(strftime('%s', d2) - strftime('%s', d1), 'unixepoch'), 4);
Example:
d1 = '2004-01-01 02:34:56'
d2 = '2012-01-01 12:00:00'
sqlite> select cast((strftime('%s','2012-01-01 12:00:00') - strftime('%s','2004-01-01 02:34:56')) / 86400 *24 + cast(strftime("%H", time(strftime('%s','2012-01-01 12:00:00') - strftime('%s','2004-01-01 02:34:56'), 'unixepoch')) as int) as text)
|| ":"
|| substr(time(strftime('%s','2012-01-01 12:00:00') - strftime('%s','2004-01-01 02:34:56'), 'unixepoch'), 4);
70137:25:04

Resources