Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert interval to minutes

Tags:

oracle

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?

like image 441
zerkms Avatar asked Feb 21 '11 07:02

zerkms


People also ask

How to convert interval to minutes Postgres?

Get the Total Number of Minutes in PostgreSQLEXTRACT(EPOCH FROM interval_value) returns the number of seconds, then the result is divided by 60 to get the number of minutes.

What is interval day to second?

Oracle INTERVAL DAY TO SECOND data type The INTERVAL DAY TO SECOND stores a period of time in terms of days, hours, minutes, and seconds. In this syntax: – day_precision is the number of digits in the DAY field. It ranges from 0 to 9.

What is interval conversion?

An interval is composed of one or more datetime elements. For example, you might choose to express an interval in terms of years and months, or you might choose to speak in terms of hours and minutes. Table 10-2 lists the standard names for each of the datetime elements used to express intervals.

What is interval in Oracle?

Oracle's two INTERVAL data types are used to store a period of time. There are a few functions that convert data into these data types.


1 Answers

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.

like image 108
Rob van Wijk Avatar answered Sep 22 '22 20:09

Rob van Wijk