`
stop_
  • 浏览: 49204 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

如何求得两个日期时间类型之间的天数

阅读更多

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics