简体   繁体   English

将分钟转换为小时:Oracle SQL 中的分钟

[英]Convert minutes to Hours:Minutes in Oracle SQL

Morning Team,晨队,

I have an Oracle SQL script that is calculating from creation of an event and how many minutes old compared to the systimestamp.我有一个 Oracle SQL 脚本,该脚本从事件的创建以及与系统时间戳相比的多少分钟开始计算。 I need to convert the minutes, which are coming out as 120 for 2 hour for example, into Hours:Minutes version, ie 2:00我需要将分钟(例如 2 小时为 120)转换为 Hours:Minutes 版本,即 2:00

I'm struggling with that part and would like to ask if someone could help?我正在努力解决这部分问题,想问是否有人可以提供帮助? My current code for the calculation is:我当前的计算代码是:

(ROUND(((cast(systimestamp as date) - cast(n.createdttm as date)))*1440,0)) "Minutes old",

I'm sure it's something simple but with all my fiddling I am not able to get it.我确信这很简单,但我的所有摆弄我都无法得到它。

Thank you谢谢

You can create an INTERVAL form the minutes.您可以创建一个INTERVAL形式的分钟。

select numtodsinterval(120, 'minute') from dual

Or create a datetime from the minutes and convert its time part to a string of hours and minutes.或者从分钟创建一个日期时间,并将其时间部分转换为一串小时和分钟。

select to_char(trunc(sysdate) + interval '1' minute * 120, 'hh24:mi') from dual

It looks like createdttm is a timestamp, so you can just subtract:看起来createdttm是一个时间戳,所以你可以减去:

systimestamp - createdtm

... to get an interval value like +000000000 02:00:00.00000 . ...获得像+000000000 02:00:00.00000这样的间隔值。 You can't format that directly, but you can either extract the various elements and concatenate those back together, or treat it as a string and cut out the bits you want.您不能直接对其进行格式化,但您可以提取各种元素并将它们重新连接在一起,或者将其视为字符串并删除您想要的位。

If you only want the time part and it will always be less than a day you can just do:如果您只想要时间部分并且总是少于一天,您可以这样做:

substr(systimestamp - createdtm, 12, 5)

02:00

But if it can go over 24 hours then you probably want the day part too, which you could still get just with substr (and maybe replace to change the space to another colon) if you know it can never be more than 2 days:但是,如果它可以 go 超过 24 小时,那么您可能也想要白天部分,如果您知道它永远不会超过 2 天,您仍然可以使用 substr 获得它(并且可能替换为另一个冒号):

substr(systimestamp - createdtm, 10, 7)

0 02:00

That's unlikely to be a safe assumption though, so instead you could extract the number of days and concatenate that:不过,这不太可能是一个安全的假设,因此您可以提取天数并将其连接起来:

extract(day from (systimestamp - createdtm)) || ':' || substr(systimestamp - createdtm, 12, 5)

0:02:00

You could only show the number of days if it's non-zero, but that would probably be quite confusing to who/whatever is looking at the results;如果天数不为零,您只能显示天数,但这可能会让查看结果的人/任何人感到非常困惑; but if you really wanted to:但如果你真的想:

case when extract(day from (systimestamp - createdtm)) > 0
     then extract(day from (systimestamp - createdtm)) || ':'
end || substr(systimestamp - createdtm, 12, 5)

02:00

db<>fiddle with a few sample values. db<> 摆弄一些样本值。

One thing to note is this effectively truncates the seconds off the time;需要注意的一点是,这有效地缩短了时间的秒数。 your original attempt included round() , but that might not have been what you meant.您最初的尝试包括round() ,但这可能不是您的意思。

If u want to convert minutes to hours and minutes.如果你想将分钟转换为小时和分钟。 If x is the number of minutes (such as 350):如果 x 是分钟数(例如 350):

TO_CHAR ( FLOOR (x / 60)) || ':'
                  || TO_CHAR ( MOD (x, 60)
                         , 'FM00'
                     )

If u want to convert hours and minutes to minutes.如果你想将小时和分钟转换为分钟。

SELECT  (TRUNC (x) * 60) + 
    ( (MOD (x, 1)
    * 100 
    ) 
FROM    dual;

where x is a NUMBER.其中 x 是一个数字。 If you have a sting, s, instead, use TO_NUMBER (s) in place of x.如果你有一个 s 字符串,请使用 TO_NUMBER (s) 代替 x。

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

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