如何获得两个日期之间的小时数差异? [英] How can I get the difference in hours between two dates?
本文介绍了如何获得两个日期之间的小时数差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须执行选择",以返回数据库中日期类型的字段与当前日期(以小时为单位)之间的差.我该如何在oracle中做到这一点?
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?
我尝试:
select 24 * (to_date(sysdate, 'YYYY-MM-DD hh24:mi')
- to_date('2012-02-28 15:20', 'YYYY-MM-DD hh24:mi')) diff_hours
from dual;
但是那没用.
推荐答案
错误是因为SYSDATE已经是日期,所以无需使用TO_DATE()
将其转换为日期.
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;
这篇关于如何获得两个日期之间的小时数差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文