DATEDIFF function in Oracle [duplicate] - sql

This question already has answers here:
Calculate difference between 2 date / times in Oracle SQL
(21 answers)
Closed last year.
I need to use Oracle but DATEDIFF function doesn't work in Oracle DB.
How to write the following code in Oracle? I saw some examples using INTERVAL or TRUNC.
SELECT DATEDIFF ('2000-01-01','2000-01-02') AS DateDiff;

In Oracle, you can simply subtract two dates and get the difference in days. Also note that unlike SQL Server or MySQL, in Oracle you cannot perform a select statement without a from clause. One way around this is to use the builtin dummy table, dual:
SELECT TO_DATE('2000-01-02', 'YYYY-MM-DD') -
TO_DATE('2000-01-01', 'YYYY-MM-DD') AS DateDiff
FROM dual

Just subtract the two dates:
select date '2000-01-02' - date '2000-01-01' as dateDiff
from dual;
The result will be the difference in days.
More details are in the manual:
https://docs.oracle.com/cd/E11882_01/server.112/e41084/sql_elements001.htm#i48042

You can simply subtract two dates. You have to cast it first, using to_date:
select to_date('2000-01-01', 'yyyy-MM-dd')
- to_date('2000-01-02', 'yyyy-MM-dd')
datediff
from dual
;
The result is in days, to the difference of these two dates is -1 (you could swap the two dates if you like). If you like to have it in hours, just multiply the result with 24.

We can directly subtract dates to get difference in Days.
SET SERVEROUTPUT ON ;
DECLARE
V_VAR NUMBER;
BEGIN
V_VAR:=TO_DATE('2000-01-02', 'YYYY-MM-DD') - TO_DATE('2000-01-01', 'YYYY-MM-DD') ;
DBMS_OUTPUT.PUT_LINE(V_VAR);
END;

Related

Dateadd and Datediff function in oracle

I want to use Oracle but DATEDIFF and DATEADD function doesn't work in Oracle DB.
How to write below mentioned code in Oracle?
datediff('QUARTER', pr.StartDate, SYSDATE)
datediff('MONTH', pr.StartDate, SYSDATE)
The best thing to do in this case is to use Oracle's MONTHS_BETWEEN() function.
Instead of:
datediff('QUARTER', pr.StartDate, SYSDATE)
you would use:
MONTHS_BETWEEN(pr.startdate, SYSDATE) / 3
and instead of:
datediff('MONTH', pr.StartDate, SYSDATE)
you would use:
MONTHS_BETWEEN(pr.startdate, SYSDATE)
Keep in mind that MONTHS_BETWEEN() will return fractions of months, so use TRUNC() or ROUND() if you need an integer number.
Of course, if you need days instead of months, you can simply subtract one date from another, e.g., SYSDATE - pr.startdate or vice-versa.
If you need to add days to a date, you can simply to this:
pr.startdate + 1 -- where 1 is the number of days
And if you need to add or subtract months, use the ADD_MONTHS() function - unlike INTERVALs this function is safe to use in leap years.
Hope this helps.
These do not have simple exact equivalents because of the semantics of datediff(). It counts the number of "boundaries" between two date/times.
I believe these are semantically equivalent:
trunc(months_between(trunc(pr.StartDate, 'Q'), trunc(sysdate, 'Q')) / 3)
months_between(trunc(pr.StartDate, 'MONTH'), trunc(sysdate, 'MONTH'))
Because of the truncation to the first day of the quarter/month, these should not be returning fractions.

turnaround time calculation in oracle SQL developer

I am trying to calculate turnaround time in days between 2 dates for each record.
the first date (ORDERDATE), which is a string that I converted to date format
using To_char(to_date) function
the second date is (CURRENT_DATE) which has proper date format.
SELECT
SPECCODE,
SOURCECODE,
SOURCEDESCRIPTION,
**TO_CHAR( TO_DATE (PATCASE.ORDEREDDATE, 'YYYYMMDD'))"ORDER_DATE",
CURRENT_DATE**
FROM ...........
You could use simple substraction:
SELECT
TRUNC(TO_DATE (PATCASE.ORDEREDDATE, 'YYYYMMDD') - SYSDATE) AS days_diff
FROM ...;
DBFiddle Demo

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.

Oracle query concatenate date

I have the following query:
select *
from mytable
where to_char(mydate,'mm/dd/yyyy') between ('05/23/2013')
and ('06/22/2013')
I need to change it to make dynamically so that I won't modify it every month from 05/23/2013 to 06/23/2013 for example:
('05/23/' + (select to_char(sysdate, 'yyyy') from dual))
but this is giving an error. Any suggestions?
What I need to do: Every month I need to run this query to get the records between 23rd of this month and 23rd of the last month.
Oracle uses || as the concatenation operator:
('05/23/' || (select to_char(sysdate, 'yyyy') from dual))
BTW, David is right. If you really want to compare string representations of dates (but why?), use a date format that is ordered the same way as dates:
to_char(mydate,'yyyy/mm/dd')
You're performing a comparison on strings, not on dates, so your code doesn't work the way you think it does.
Based on the string logic, "05/23/2000" is between "05/22/2013" and "06/24/2000".
Keep the data types as date and Oracle will get the comparison right.
Possibly what you want is:
select *
from my_table
where mydate >= add_months(trunc(sysdate,'MM'),-1)+22 and
mydate < trunc(sysdate,'MM')+22
but it's difficult to tell without a description of what the requirement actually is.
How about this one?
SELECT '(''05/23/'''||to_char(sysdate, 'yyyy')||'''' FROM DUAL
Have not testet because I have no Oracle database right now, needs checking for quote escapes...
Do you need exact days from the month? You can also substract days from sysdate:
SELECT (sysdate - 30) FROM DUAL
You may also use concat function
concat('05/23/', (select to_char(sysdate, 'yyyy') from dual))

Oracle equivalent to SQL Server DATEPART

We need to get the HOUR out of a DATETIME column (expecting values from 0 to 23 to be returned).
Is there an Oracle equivalent of the SQL Server DATEPART function?
An alternative is the EXTRACT function which is an ANSI standard and also works on other DBMS, but it also requires the use of current_timestamp (also ANSI) instead of sysdate
SELECT extract(hour from current_timestamp)
FROM dual
SELECT to_number(to_char(sysdate, 'HH24')) FROM DUAL
I think this is what you are looking for
select to_char(current_timestamp,'HH24') from dual;
for additional ways of using DATEPART kind of function in oracle
DATEPART:-Weekday
--This would give the day for the week for a reference start day
DECLARE
Start_day date:=to_date('30-03-2012','DD-MM-YYYY');
check_date DATE:=SYSDATE;
Day_of_week NUMBER;
i NUMBER;
BEGIN
i:=to_char(Start_day,'D');
day_of_week:=mod((to_char(check_date,'D') -(i-1)+7),7);
if day_of_week=0
THEN
day_of_week:=7;
end if;
dbms_output.put_line(day_of_week);
END;
if you want to get the month name of a date
select to_char( 'month', sysdate())