Oracle 日期差异 SQL 查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14217889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:02:07  来源:igfitidea点击:

Oracle date difference SQL query

sqloracledate-arithmetic

提问by Ussamah Habib

Possible Duplicate:
Calculate difference between 2 date / times in Oracle SQL

可能的重复:
计算 Oracle SQL 中 2 个日期/时间之间的差异

I have a table where PurchasedDate is maintained. I have to get the date from the system and calculate the date difference in days. datediff() is not giving tHe result as I am working in Oracle.

我有一个表,其中维护了 PurchasedDate。我必须从系统中获取日期并计算以天为单位的日期差异。datediff() 没有给出结果,因为我在 Oracle 工作。

Sample table:

示例表:

ProductID  Date
P101       31-DEC-2012
P102       29-DEC-2011

If P102 is returned today, the difference should be 10.

如果今天返回 P102,则差值应为 10。

Here is a detailed description

这里有详细说明

BillID     ProductID   Date
101          P101        31-DEC-2012
101          P102        31-DEC-2012
102          P405        29-JUN-2012
102          P210        29-JUN-2012
105          P205         2-MAY-2012

If a customer comes to retun a product P101 with BillNo 101, then the system should calcualte the difference between the return date(which is the system date) and the purchased date. The answer for P101 should be 10 if its returned on Jan 8 2012

如果客户以BillNo 101 来退回产品P101,则系统应计算退货日期(即系统日期)与购买日期之间的差值。如果 P101 于 2012 年 1 月 8 日返回,则其答案应为 10

回答by A.B.Cade

try

尝试

SELECT trunc(sysdate) - PurchasedDate 
  FROM table_name;

回答by DazzaL

in Oracle you can just subtract the dates. this will give you an answer in days.

在 Oracle 中,您可以减去日期。这将在几天内给您答复。

eg assuming you meant 29-dec-2012 and not 29-dec-2011!

例如,假设您的意思是 29-dec-2012 而不是 29-dec-2011!

select trunc(sysdate) - your_date from your_table;


SQL> select trunc(sysdate) - to_date('29-dec-2012', 'dd-mon-yyyy') from dual;

TRUNC(SYSDATE)-TO_DATE('29-DEC
------------------------------
                            10

回答by Sri

you can find good information about calculating date differences in oracle from below link

您可以从以下链接中找到有关在 oracle 中计算日期差异的好信息

How To Calculate Difference Between Dates in Oracle SQL

如何计算 Oracle SQL 中日期之间的差异