如何求得两个日期时间类型之间的天数
lastfhdate,orderdate是时间日期型
错误的求法:
用to_date函数
select lastfhdate,orderdate, to_date(lastfhdate,'yyyy-mm-dd') - to_date( orderdate,'yyyy-mm-dd') from v_xc_orderdetail
结果如下:
2007-07-25 20:36:07 2007-06-28 16:59:09 -1066
结果明显不对(两个时间相差不到3个月)。
查找oracle的文档。
发现to_date的第一个参数是char,不能是 时间日期型,其中有这样一句:
Do not use the TO_DATE function with a DATE value for the char argument.。
又执行如下语句:
select to_date(lastfhdate,'yyyy-mm-dd') , to_date( orderdate,'yyyy-mm-dd') from v_xc_orderdetail
结果如下:
0025-07-07 0028-06-07
原来把天处理成了年份,年份的后两位成了天。
接着执行如下的语句
select to_date('0025-07-07','yyyy-mm-dd') - to_date('0028-06-07','yyyy-mm-dd') from dual
结果就 是
-1066
正确的求法
1.两个日期时间直接相减,然后求整数
select Floor(lastFhDate - orderDate) from v_xc_orderDetail
2.用trunc函数
select trunc(lastfhdate) - trunc(orderdate) from dual