1

当我使用

select trunc(sysdate) from dual

我得到了正确的日期,今天的格式很好。像“2013 年 6 月 24 日”

如果我这样做

select trunc(sysdate)-1 from dual

我昨天的约会格式很好。

但是,如果我这样做:

select trunc(sysdate)- trunc(sysdate-1) from dual

我想得到 1,但我得到的是/d/yyyy

我要做的就是计算两个日期之间的天数。通过使用Trunc(date) - Trunc(another date),我总是得到/d/yyyy而不是这两个日期之间的天数。

怎么能这样做?

提前致谢。

4

2 回答 2

1

Oracle has a days_between function: DAYS_BETWEEN(date1, date2), so your query could be expressed as:

select abs(days_between(trunc(sysdate),trunc(sysdate-1))) from dual

I have included the abs() function as I can't remember off hand whether the most recent date should go first or second to get a positive result and don't have access to check right now.

于 2013-06-24T20:36:01.017 回答
0

Just use minus (-) Oracle stores dates as numbers and 1 = 1day

so sysdate + 1 is tomorrow, sysdate - 1 is yesterday

From SQL fiddle TRUNC(SYSDATE)-TRUNC(SYSDATE-1) 1

于 2013-06-24T20:23:00.933 回答