简体   繁体   English

Oracle-查询两个日期之间的时差

[英]Oracle - Query the difference between two dates

I am trying to come up with a way in Oracle to calculate the difference between dates in days. 我试图在Oracle中提出一种方法来计算日期之间的天数差异。

In short. 简而言之。 I have two data fields "begins_at" and "ends_at" and the DATA_TYPE for those fields is "TIMESTAMP(6)" 我有两个数据字段“ begins_at”和“ ends_at”,这些字段的DATA_TYPE为“ TIMESTAMP(6)”

For example. 例如。 I have the following scenario: 我有以下情况:

"begins_at" value as “26-MAR-13 02.50.00.000000000 PM” “ begins_at”值为“ 13年3月26日02.50.00.000000000 PM”

"ends_at" value as “30-MAR-13 09.25.00.000000000 PM” “ ends_at”值为“ 13年3月30日09.25.00.000000000 PM”

I am trying to calculate the different between the two timestamps in days. 我正在尝试计算几天内两个时间戳之间的差异。 Is it possible in Oracle? 在Oracle中可能吗?

I tried the following statement but threw an error: 我尝试了以下语句,但引发了错误:

Select TO_DATE(begins_at, 'YYYYMMDD HH:MI:SS AM') - TO_DATE(ends_at, 'YYYYMMDD   HH:MI:SS AM') day_diff

From dual

Any tip is much appreciated. 任何提示,不胜感激。

As our anonymous equine friend noted, 'threw an error' is not helpful. 正如我们的匿名马友指出的那样,“抛出错误”没有帮助。 However, it's likely you are getting an ORA-01830, 'date format picture ends before converting entire input string', or ORA-1858, 'a non-numeric character was found where a numeric was expected', or something similar. 但是,您可能会收到ORA-01830,“日期格式图片在转换整个输入字符串之前结束”或ORA-1858,“在预期有数字的地方找到了非数字字符”或类似的内容。

The to_date function takes a char argument, so when you pass your timestamp field to it there is an implicit conversion happening; to_date函数带有一个char参数,因此当您将timestamp字段传递给它时,会发生隐式转换。 you're essentially doing: 您实际上是在做:

to_date(to_char(begins_at, 'DD-MON-RR'), 'YYYYMMDD HH:MI:SS AM')

... only using your default NLS settings; ...仅使用您的默认NLS设置; I'm guessing you're NLS_DATE_FORMAT is set to 'DD-MON-RR' for this example; 我猜您将NLS_DATE_FORMAT设置为“ DD-MON-RR”。 the exact value will affect the error you get. 确切的值将影响您得到的错误。 The format that your timestamp is being given as a string does not match the format you specified for to_date . timestamp以字符串形式给出的格式与您为to_date指定的格式不匹配。

You should never rely on implicit conversions, but you don't need to do a conversion here at all. 您永远不要依赖隐式转换,但是您根本不需要在这里进行转换。 timestamp values can be compared directly, and the results are explained in the documentation . timestamp值可以直接进行比较,结果在文档中进行了说明 Just subtracting one value form the other will give you the answer as an interval data type: 只需从另一个值中减去一个值,就可以得出interval数据类型的答案:

select ends_at - begins_at from t42;

ENDS_AT-BEGINS_AT
---------------------------------------------------------------------------
+000000004 06:35:00.000000

If you just want the whole day portion you can use extract : 如果您只需要一整天,可以使用extract

select extract (day from ends_at - begins_at) from t42; 从t42中选择摘录(days_at-Begins_at);

EXTRACT(DAYFROMENDS_AT-BEGINS_AT)
---------------------------------
                                4

You can also treat the timestamp as date using cast : 您还可以使用casttimestamp视为date

select cast(ends_at as date) - cast(begins_at as date) from t42;

CAST(ENDS_ATASDATE)-CAST(BEGINS_ATASDATE)
-----------------------------------------
                               4.27430556

I'm not sure why you're using timestamp at all though. 我不确定您为什么要使用timestamp The Oracle date type includes a time portion , down to a precision of a second. Oracle date类型包括一个时间部分 ,精确到秒。 As you don't (currently) seem to be using the fractional part of the timestamp, sticking to date might make your manipulations simpler, but I guess it depends what you intend to do with these fields later. 由于您(当前)似乎没有使用时间戳的小数部分,因此坚持使用date可能会使您的操作更简单,但是我想这取决于您稍后打算对这些字段进行的处理。

See http://docs.oracle.com/cd/E17952_01/refman-5.1-en/date-and-time-functions.html#function_datediff . 请参阅http://docs.oracle.com/cd/E17952_01/refman-5.1-en/date-and-time-functions.html#function_datediff It describes DATEDIFF , which returns the difference in days, treating only the date values. 它描述了DATEDIFF ,它以天为单位返回差值,仅处理日期值。

Try something like this: 尝试这样的事情:

SELECT  TO_DATE('20120125 11:00:00 AM', 'YYYYMMDD HH:MI:SS AM')
     - TO_DATE('20120120 10:00:00 AM', 'YYYYMMDD HH:MI:SS AM') day_diff
FROM   dual

You can refer this . 你可以参考这个 It will really help you 真的会对你有帮助

Days, hours, minutes, seconds diff. 天,小时,分钟,秒的差异。 example - copy/paste to see the output. 示例-复制/粘贴以查看输出。 Replace dates with your dates...: 用您的日期替换日期...:

SELECT start_date, end_date, time_diff,
       EXTRACT(DAY FROM time_diff) days,
       EXTRACT(HOUR FROM time_diff) hours,
       EXTRACT(MINUTE FROM time_diff) minutes,
       EXTRACT(SECOND FROM time_diff) seconds
  FROM
  (
   Select start_date, end_date, (end_date - start_date) time_diff
     From
     (
      Select CAST(to_date('01/01/2012 10:00:00 am', 'dd/mm/yyyy  hh:mi:ss am') AS TIMESTAMP) end_date
           , CAST(to_date('01/01/2012 07:00:00 am', 'dd/mm/yyyy  hh:mi:ss am') AS TIMESTAMP) start_date
       From dual
     )
   )
  /

Subtract time only example: 仅减去时间的示例:

SELECT trunc(mydate / 3600) hr
     , trunc(mod(mydate, 3600) / 60) mnt
     , trunc(mod(mydate, 3600) / 60 /60) sec
  FROM 
  (
   SELECT (to_date('01/03/2012 10:00:00', 'mm/dd/yyyy  hh24:mi:ss') -
           to_date('01/01/2012 07:00:00', 'mm/dd/yyyy  hh24:mi:ss')) * 86400 mydate
     FROM dual
  )
 /

HR  MNT SEC
-------------
51  0   0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM