get the date difference in milliseconds in oracle - datetime

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.

Related

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;

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

Is there an equivalent of Sql Server's DateDiff('ms', d1, d2) in Sqlite?

I am trying to fathom if it is possible to calculate the number of elapsed milliseconds between dates in Sqlite. ( DateDiff('milliseconds', d1, d2) in SQL Server)
There's a great example on how to do it for seconds here, but not for milliseconds.
You can get there.
Given these 2 dates: 12:00:00:000 & 12:05:01:200
( (
(strftime('%M', EndDate) * 60) -
(strftime('%M', StartDate) * 60)
) +
strftime('%f', EndDate) -
strftime('%f', StartDate)
) * 1000
results in a diff off 301200 (milliseconds)
The key is getting your head around the fact that %f yields seconds and milliseconds as a fraction of seconds (1.2), and you need to add that onto the total seconds in the minutes.

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

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

MySQL: Records inserted by hour, for the last 24 hours

I'm trying to list the number of records per hour inserted into a database for the last 24 hours. Each row displays the records inserted that hour, as well as how many hours ago it was.
Here's my query now:
SELECT COUNT(*), FLOOR( TIME_TO_SEC( TIMEDIFF( NOW(), time)) / 3600 )
FROM `records`
WHERE time > DATE_SUB(NOW(), INTERVAL 24 HOUR)
GROUP BY HOUR(time)
ORDER BY time ASC
right now it returns:
28 23
62 23
14 20
1 4
28 3
19 1
That shows two rows from 23 hours ago, when it should only show one per hour.
I think it has something to do with using NOW() instead of getting the time at the start of the hour, which I'm unsure on how to get.
There must be a simpler way of doing this.
If you grouped by HOUR(time) then you should use HOUR(time) in your select expressions, and not time. For example:
SELECT HOUR(time), COUNT(*)
FROM `records`
WHERE time > DATE_SUB(NOW(), INTERVAL 24 HOUR)
GROUP BY HOUR(time)
ORDER BY HOUR(time)
Alternatively you can group by the expression you want to return:
SELECT COUNT(*), FLOOR( TIME_TO_SEC( TIMEDIFF( NOW(), time)) / 3600 )
FROM `records`
WHERE time > DATE_SUB(NOW(), INTERVAL 24 HOUR)
GROUP BY FLOOR( TIME_TO_SEC( TIMEDIFF( NOW(), time)) / 3600 )
ORDER BY FLOOR( TIME_TO_SEC( TIMEDIFF( NOW(), time)) / 3600 )
In case you were wondering, it is safe to call NOW() multiple times in the same query like this. From the manual:
Functions that return the current date or time each are evaluated only once per query at the start of query execution. This means that multiple references to a function such as NOW() within a single query always produce the same result.

Resources