---
title: "如何求得两个日期时间类型之间的天数"
author: "stop_"
date: "2007-11-01T14:20:42Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2007-11-01T14:20:42Z"
id: 137292
url: "https://stop.iteye.com/blog/137292"
---

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**