DATEDIFF function in Oracle

前端 未结 4 1931
难免孤独
难免孤独 2020-11-30 10:55

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.

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-30 11:27

    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.

提交回复
热议问题