Using DateDiff() in Oracle

╄→尐↘猪︶ㄣ 提交于 2019-12-12 02:34:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!