How to get difference between two dates in days in postgres - sql

Following is a query in oracle.
SELECT start_date - TO_DATE('1900-01-01','YYYY-MM-DD') FROM start_table
In oracle it gives the output 44680.3646, where start_date is 01-MAY-22.
what query would require to form to get the same output in EDB and postgresql

If you want to get the fractional part of a day, then you need to convert each value to number of seconds using EXTRACT(EPOCH FROM ...) and divide by 86400(number of seconds in 1 day) and then find the difference of the results.
SELECT extract(epoch from '2022-05-01 11:44:16'::timestamp - '1900-05-02'::timestamp) / 86400 as date
Result: 44559.489074074074
Demo in DBfiddle

Related

How to calculate exact hours between two datetime fields?

I need to calculate hours between datetime fields and I can achieve it by simply doing
select date1,date2,(date1-date2) from table; --This gives answer in DD:HH:MM:SS format
select date1,date2,(trunc(date1)-trunc(date2))*24 --This doesn't take into account the time, it only gives hours between two dates.
Is there a way I can find the difference between date times that gives the output in Hours as a number?
The 'format' comment on your first query suggests your columns are timestamps, despite the dummy column names, as the result of subtracting two timestamps is an interval. Your second query is implicitly converting both timestamps to dates before subtracting them to get an answer as a number of days - which would be fractional if you weren't truncating them and thus losing the time portion.
You can extract the number of hours from the interval difference, and also 24 * the number of days if you expect it to exceed a day:
extract(day from (date1 - date2)) * 24 + extract(hour from (date1 - date2))
If you want to include fractional hours then you can extract and manipulate the minutes and seconds too.
You can also explicitly convert to dates, and truncate or floor after manipulation:
floor((cast(date1 as date) - cast(date2 as date)) * 24)
db<>fiddle demo
Use the DATEDIFF function in sql.
Example:
SELECT DATEDIFF(HOUR, '2021-09-05 12:00:00', GETDATE());
You can find it using the differnece of dates and multiplying with 24
select date1
,date2
,(date1-date2)*24 as diff_in_hrs
from table

SQL difference between two datetime columns

I have a dataset with 2 columns of datetime datatype as shown here:
I want to take the difference between the two dates and I try it with this code:
Select
*,
original_due_date - due_date as difference
from
Table
However I'm not sure if the same would suffice as this is a datetime and not just date.
Any inputs would be much appreciated.
Desired output
The question was originally tagged Postgres, so this answers the original question.
Presumably, you are storing the values as timestamps. If you just want the results in days, then convert to dates and take the difference:
Select t.*,
(t.original_due_date::date - t.due_date::date) AS difference
from Table t;
If you want fractional days, then a pretty simple method is to extract the "epoch", which is measured in seconds, and use arithmetic:
Select t.*,
( extract(epoch from t.original_due_date -
extract(epoch from t.due_date
) / (24.0 * 60 * 60) AS decimal_days
from Table t;
transform timestamps to seconds (unix_timestamp), calculate difference and divide by (60*60*24) to get days
select (unix_timestamp(original_due_date, 'MM-dd-yyyy HH:mm')-unix_timestamp(due_date, 'MM-dd-yyyy HH:mm'))/(60*60*24) as difference_days
from (select '07-01-2021 00:00' as due_date, '02-10-2020 00:00' as original_due_date) t
Result:
-507

Average Timestamp oracle with milliseconds

Hello everyOne I need to get the AVERAGE of difference of two dates (timestamp)
I tried this
select AVG((sva.endTime - sva.startTime)) as seconds from SVATable sva;
but I got an error
93/5000
ORA-00932: Inconsistent data types; expected: NUMBER; got: INTERVAL DAY TO SECOND
You may use EXTRACT to get AVG seconds.
SELECT AVG (EXTRACT (SECOND FROM (sva.endTime - sva.startTime)))
AS avg_seconds
FROM SVATable sva;
This is an insidious problem in Oracle. Your calculation would work with the date data type, but it does not work with timestamps.
One solution is to extract the days, hours, minutes, and seconds from the interval. Another is to use date arithmetic. You can get fractions of a day by using:
select (date '2000-01-01' + (sva.endTime - sva.startTime)) - date '2000-01-01'
You can use the average and convert to seconds:
select avg( (date '2000-01-01' + (sva.endTime - sva.startTime)) - date '2000-01-01') * (60*60*24)

How to write mysql TIMESTAMPDIFF function in Oracle sql query

I have query in Mysql which return minutes using TIMESTAMPDIFF in table. But now i have migrated my data to Oracle. So i want to use the same query to get the TIMESTAMPDIFF in a table in Oracle. Oracle also dont support NOW() function in mysql. The PROCESS_START_DATE column in query have data which contains date and time. I tried EXTRACT function in oraclebut did not work. Here is my query :
select * from(
select trunc(abs(to_date('27/01/2015 08:00:00','dd/mm/yyyy hh:mi:ss') - PMS.PROCESS_START_DATE)*24*60),PM.NAME,PM.ENABLED
from PROCESS_MONITOR_STATISTIC PMS
JOIN PROCESS_MONITOR PM ON PM.ID=PMS.PROCESS_MONITOR_ID
WHERE PM.ENABLED=1 AND PM.NAME= 'WORKFLOWENGINE1'
order by PMS.PROCESS_START_DATE desc
)
where ROWNUM = 1
You can do something like this:
--in case you are working with dates
select trunc(abs(to_date('26/01/2015 08:00:00','dd/mm/yyyy hh:mi:ss') - sysdate)*24*60) from dual;
This represent difference in minutes between a date and now(sysdate) with dates.
--timestamp case
select abs(
extract (day from diff)*24*60 + extract (hour from diff)*60 + extract (minute from diff)) from
(select to_timestamp('27/01/2015 09:07:00','dd/mm/yyyy hh:mi:ss') - systimestamp diff from dual);
This represent difference in minutes between a date and now(systimestamp) with timestamp.
Edit:
This query calculate minutes in a year:
select 365*24*60 from dual -- this returns 525600
This is your query. i change the time. Check that the difference between these dates is one year and five minutes
select trunc(abs((to_date('26/01/14 09:00:00','dd/mm/yy hh24:mi:ss')-
to_date('26/01/2015 09:05:01','dd/mm/yyyy hh24:mi:ss'))*24*60)) from dual;
So, when run this query result is 525605, five minutes more than a year. So it looks to be working.

difference between two timestamps (in days) in oracle

SELECT MIN (snap_id) AS FIRST_SNAP,
MAX (snap_id) AS LAST_SNAP,
MIN (BEGIN_INTERVAL_TIME) AS FIRST_QUERY,
MAX (END_INTERVAL_TIME) AS LAST_QUERY,
max(end_interval_time) - min(begin_interval_time) as "TIME_ELAPSED"
FROM dba_hist_snapshot
ORDER BY snap_id;
2931 3103 5/28/2012 6:00:11.065 AM 6/4/2012 11:00:40.967 AM +07 05:00:29.902000
I would like the last columns output to be 7 (for the days). I have tried trunc and extract like some other posts mentioned but can't seem to get the syntax right. Any ideas?
Judging from your comment, you're using timestamp columns, not datetime. You could use extract to retrieve the hour difference, and then trunc(.../24) to get the whole number of days:
trunc(extract(hour from max(end_interval_time) - min(begin_interval_time))/24)
Or you could cast the timestamp to a date:
trunc(cast(max(end_interval_time) as date) -
cast(min(begin_interval_time) as date))