How can I get the difference in hours between two dates?

后端 未结 4 1927
情歌与酒
情歌与酒 2020-12-10 18:43

I have to do a \"select\" that returns the difference between a field of type date from the database and the current date in hours. How can I do this in oracle?

I tr

4条回答
  •  隐瞒了意图╮
    2020-12-10 19:41

    The error is because SYSDATE is already a date, there's no need to use TO_DATE() to convert it to a date.

    If you don't convert it to a date:

    select
        24 * (sysdate - to_date('2012-02-28 15:20', 'YYYY-MM-DD hh24:mi')) as diff_hours
    from dual;
    

    And if the formatting of the dates are wrong, you can possible use two steps like:

    select
        24 * (to_date(to_char(sysdate, 'YYYY-MM-DD hh24:mi'), 'YYYY-MM-DD hh24:mi') - to_date('2012-02-28 15:20', 'YYYY-MM-DD hh24:mi')) as diff_hours
    from dual;
    

提交回复
热议问题