将间隔转换为分钟 [英] Convert interval to minutes

查看:82
本文介绍了将间隔转换为分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个间隔:

INTERVAL '0 00:30:00' DAY TO SECOND
INTERVAL '0 04:00:00' DAY TO SECOND

在每个间隔中获取分钟数的最优雅方法是什么. 30240相应.

What is the most elegant way to get amount of minutes in each interval. 30 and 240 accordingly.

是的,我知道我可以表演EXTRACT(HOUR FROM interval) * 60 + EXTRACT(MINUTE FROM interval),但这对我来说很糟糕.

Yes, I know I can perform EXTRACT(HOUR FROM interval) * 60 + EXTRACT(MINUTE FROM interval), but this looks terrible to me.

有更好的解决方案吗?

推荐答案

您看上去很糟糕的东西,对我来说似乎完全可以接受.如果您以算术方式查看文档,则可以在INTERVAL上执行:

What looks terrible to you, looks perfectly acceptable to me. If you look at the documentation at the arithmetic you can perform on INTERVALs:

http://download.oracle. com/docs/cd/E11882_01/server.112/e17118/sql_elements001.htm#sthref175

然后您可以将它们与数字相乘.因此,如果将时间间隔乘以24和60,则可以通过提取天数来获得分钟数.它更紧凑,但我怀疑您认为它是否更优雅.

then you see you can multiply them with numerics. So if you multiply your intervals to 24 and 60, you can get the number of minutes by extracting the number of days. It's more compact, but I doubt if it's more elegant in your view.

SQL> create table t (my_interval interval day to second)
  2  /

Table created.

SQL> insert into t
  2  select numtodsinterval(30,'minute') from dual union all
  3  select numtodsinterval(4,'hour') from dual
  4  /

2 rows created.

SQL> select my_interval
  2       , 60 * extract(hour from my_interval)
  3         + extract(minute from my_interval) minutes_terrible_way
  4       , extract(day from 24*60*my_interval) minutes_other_way
  5    from t
  6  /

MY_INTERVAL                    MINUTES_TERRIBLE_WAY MINUTES_OTHER_WAY
------------------------------ -------------------- -----------------
+00 00:30:00.000000                              30                30
+00 04:00:00.000000                             240               240

2 rows selected.

关于,
罗布.

Regards,
Rob.

这篇关于将间隔转换为分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆