Dateadd and Datediff function in oracle - sql

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.

Related

subtracting two years with to_char and to_number

I need to subtract two dates. First I convert the date field to years using to_char, then convert to a number using to_number. However I get an error in the query when subtracting two years:
to_char((order date),'yyyy')
to_char((beginning of the year),'yyyy')
I tried converting to a number using
to_number(to_char((order date),'yyyy'),'9999')
to_number(to_char((beginning of the year),'yyyy'),'9999')
Use extract; it is simpler than what you're doing:
select extract(year from order_date) - extract(year from trunc(sysdate, 'yyyy')) as result
from some_table
You can use the EXTRACT function:
EXTRACT(YEAR FROM SYSDATE) - EXTRACT(YEAR FROM order_date)
Or, if you want to get the actual difference and measure full years (accounting for months and not just years), then you can use MONTHS_BETWEEN and divide by 12 and round down:
FLOOR(MONTHS_BETWEEN(TRUNC(SYSDATE, 'YYYY'), order_date) / 12)

DATEDIFF function in Oracle [duplicate]

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;

Oracle SQL - Using SysDate to create a specific date

I need to automate a report that will always the first day of the current month and year in the WHERE caluses.
"YYYY-MM-01"
I am trying to achieve the following but sadly, I can't get anything to work using To_Char/ To_Date/SysDate.
to_date(SYSDATE('YYYY-MM') + '-01', 'yyyy-mm-dd')
Thank you
To get the first day of the current month (as a DATE), simply:
trunc(sysdate,'MON')
or, formatted:
to_char(trunc(sysdate,'MON'),'yyyy-mm-dd')
The string concatenation operator in Oracle is ||, not +. And SYSDATE is a constant not a function:
to_date(to_char(SYSDATE, 'YYYY-MM') || '-01', 'yyyy-mm-dd')
By the way, you can also do this calculation as:
trunc(sysdate - extract(day from sysdate) + 1)
That is, subtract the day of the month and add one. That way, you don't have to mess with fiddly date/character formats.

How to convert Date-time into Date using Netezza

I am doing some calculation but my calculation is off because my date field is showing the time-stamp and i only want to use as Date only when i am doing the calculation. How can i just ignore the minutes and just use the date when doing the calculation? Here is what i have:
SELECT EF.DSCH_TS,
CASE WHEN EXTRACT (DAY FROM EF.DSCH_TS - EF.ADMT_TS)>=0 THEN 'GroupA' END AS CAL
FROM MainTable EF;
Netezza has built-in function for this by simply using:
SELECT DATE(STATUS_DATE) AS DATE,
COUNT(*) AS NUMBER_OF_
FROM X
GROUP BY DATE(STATUS_DATE)
ORDER BY DATE(STATUS_DATE) ASC
This will return just the date portion of the timetamp and much more useful than casting it to a string with TO_CHAR() because it will work in GROUP BY, HAVING, and with other netezza date functions. (Where as the TO_CHAR method will not)
Also, the DATE_TRUNC() function will pull a specific value out of Timestamp ('Day', 'Month, 'Year', etc..) but not more than one of these without multiple functions and concatenate.
DATE() is the perfect and simple answer to this and I am surprised to see so many misleading answers to this question on Stack. I see TO_DATE a lot, which is Oracle's function for this but will not work on Netezza.
With your query, assuming that you're interested in the days between midnight to midnight of the two timestamps, it would look something like this:
SELECT EF.DSCH_TS,
CASE
WHEN EXTRACT (DAY FROM (DATE(EF.DSCH_TS) - DATE(EF.ADMT_TS)))>=0 THEN 'GroupA'
END AS CAL
FROM MainTable EF;
You may want to consider rewriting your case statement to return an interval. This will allow for a little more flexibility.
SELECT EF.DSCH_TS,
CASE
WHEN age(date(EF.DSCH_TS),date(EF.ADMT_TS))>= interval '6 days'
THEN 'GroupA' END AS CAL
FROM MainTable EF;
Use date_trunc() with the first argument of 'day'. I think this is what you want:
SELECT EF.DSCH_TS,
(case when date_trunc('day', EF.DSCH_TS) >= date_trunc('day', EF.ADMT_TS) THEN 'GroupA' END) AS CAL
FROM MainTable EF;

current_date usage

I would like to subtract the current date from a given date in SQL or in JDBC. I would like to have the result in hours. Not sure how to treat the date in that case. Can some one give me a basic example Please.
You don't mention which database server you use - here's a sample in MySQL.
select hour(TIMEDIFF('2011-03-15 19:59:59.000001', now()))
Note: the "hour" function doesn't deal with rounding, so if you need that, you need to do some further arithmetic.
Date functions are pretty vendor-specific, so your mileage may vary....
In standard SQL
select (date '2011-03-16' - CURRENT_DATE) as days_different,
(date '2011-03-16' - CURRENT_DATE) * 24 as hours_different
days_different hours_different
--
1 24
As I write this, CURRENT_DATE = '2011-03-15'.