Oracle - Best SELECT statement for getting the difference in minutes between two DateTime columns?

瘦欲@ 提交于 2019-11-27 02:40:40

问题


I'm attempting to fulfill a rather difficult reporting request from a client, and I need to find away to get the difference between two DateTime columns in minutes. I've attempted to use trunc and round with various formats and can't seem to come up with a combination that makes sense. Is there an elegant way to do this? If not, is there any way to do this?


回答1:


SELECT date1 - date2
  FROM some_table

returns a difference in days. Multiply by 24 to get a difference in hours and 24*60 to get minutes. So

SELECT (date1 - date2) * 24 * 60 difference_in_minutes
  FROM some_table

should be what you're looking for




回答2:


By default, oracle date subtraction returns a result in # of days.

So just multiply by 24 to get # of hours, and again by 60 for # of minutes.

Example:

select
  round((second_date - first_date) * (60 * 24),2) as time_in_minutes
from
  (
  select
    to_date('01/01/2008 01:30:00 PM','mm/dd/yyyy hh:mi:ss am') as first_date
   ,to_date('01/06/2008 01:35:00 PM','mm/dd/yyyy HH:MI:SS AM') as second_date
  from
    dual
  ) test_data



回答3:


http://asktom.oracle.com/tkyte/Misc/DateDiff.html - link dead as of 2012-01-30

Looks like this is the resource:

http://asktom.oracle.com/pls/asktom/ASKTOM.download_file?p_file=6551242712657900129



来源:https://stackoverflow.com/questions/206222/oracle-best-select-statement-for-getting-the-difference-in-minutes-between-two

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!