简体   繁体   English

日期和时间转换为小时和分钟

[英]Date and Time conversion to hours and minutes

In SQL developer, I substracted two timestamp columns which gave me the difference.在 SQL 开发人员中,我减去了两个timestamp列,这给了我差异。

Select (created_dt_1 - created_dt_2) as delivery_time
from order;

DELIVERY_TIME
+02 05:12:30.651000

The output is saying it took 2 days and 5 hours and 12 minutes and 30 seconds.输出说它花了 2 天 5 小时 12 分 30 秒。 However, I want it to be in hours and minutes only.但是,我希望它只在几小时和几分钟内。 I was thinking about using extract to take the days and multiply by 24 hours from created_dt_1 and created_dt_2 then using extract again for hours and minutes and finally adding them all up.我正在考虑使用提取物来花费几天时间并乘以来自 created_dt_1 和 created_dt_2 的 24 小时,然后再次使用提取物数小时和数分钟,最后将它们全部加起来。

However, I would like to know if there is a function or more efficient way of converting the 'delivery_time' into hours and minutes.但是,我想知道是否有将“delivery_time”转换为小时和分钟的函数或更有效的方法。

I was thinking about using extract to take the days and multiply by 24 hours from created_dt_1 and created_dt_2 then using extract again for hours and minutes and finally adding them all up.我正在考虑使用提取物来花费几天时间并乘以来自 created_dt_1 和 created_dt_2 的 24 小时,然后再次使用提取物数小时和数分钟,最后将它们全部加起来。

If you only want the time in minutes then:如果您只想要以分钟为单位的时间,那么:

SELECT TRUNC((CAST(created_dt_1 AS DATE) - CAST(created_dt_2 AS DATE)) * 24 * 60)
         AS delivery_time_minutes
FROM   orders;

If you want it as hours and minutes then:如果你想要小时和分钟,那么:

SELECT (EXTRACT(DAY FROM delivery_time) * 24
         + EXTRACT(HOUR FROM delivery_time)) || ' hours '
         || EXTRACT(MINUTE FROM delivery_time) || ' minutes'
         AS delivery_time
FROM   (
  SELECT (created_dt_1 - created_dt_2) AS delivery_time
  FROM   orders
);

db<>fiddle here db<> 在这里摆弄

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

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