将间隔转换为分钟

发布于 2024-10-18 11:48:19 字数 303 浏览 1 评论 0原文

假设我有 2 个间隔:

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

获取每个间隔的分钟数的最优雅的方法是什么。相应地,30240

是的,我知道我可以执行 EXTRACT(HOUR FROM Interval) * 60 + EXTRACT(MINUTE FROM Interval),但这对我来说看起来很糟糕。

还有更好的解决方案吗?

Let's suppose I have 2 intervals:

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

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

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

Any better solutions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

入怼 2024-10-25 11:48:19

在你看来很糟糕的事情,在我看来完全可以接受。如果您查看可以在 INTERVAL 上执行的算术的文档:

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

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

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.

问候,
抢。

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

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.

待天淡蓝洁白时 2024-10-25 11:48:19
(extract(day from my_interval) * 24 * 60 + extract(hour from my_interval)) * 60 + extract(minute from my_interval) my_way

别忘了几天。

(extract(day from my_interval) * 24 * 60 + extract(hour from my_interval)) * 60 + extract(minute from my_interval) my_way

Don't forget about days.

离笑几人歌 2024-10-25 11:48:19

重用上面 Rob 的例子,

select (sysdate-(sysdate-to_dsinterval(my_interval)))*24*60 from t;

Reusing Rob's example above,

select (sysdate-(sysdate-to_dsinterval(my_interval)))*24*60 from t;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文