问题
Please consider the following query. I've realised that using fans.checkout-fans.checkin
to calculate the difference between two dates (in days) is not a very good idea. How can I rewrite it so that I can use DateDiff()
instead? I can't seem to simply replace fans.checkout-fans.checkin
with this function.
select x.name
from (
select fans.name,
dense_rank() over (order by fans.checkout-fans.checkin desc) as rnk
from fans
where fans.checkout-fans.checkin is not null
) x
where x.rnk = 1
回答1:
Why do you believe that it is not a good idea to subtract two dates to get the number of days between them? That's certainly the standard way to do that sort of date arithmetic in Oracle.
DateDiff
is not a function that exists in Oracle. I know it exists in SQL Server. You could, of course, write your own function and call that
CREATE OR REPLACE FUNCTION dateDiff( p_dt1 IN DATE,
p_dt2 IN DATE )
RETURN NUMBER
IS
BEGIN
RETURN p_dt1 - p_dt2;
END;
It's not obvious, though, what benefit you derive from doing this rather than simply continuing to subtract the two dates.
回答2:
try this one :
NUMTODSINTERVAL(SYSDATE - @OLD_DT , 'DAY')
OR
TRUNC (SYSDATE) - @OLD_DT
来源:https://stackoverflow.com/questions/16493474/using-datediff-in-oracle