Oracle PL/SQL Tutorial/SQL Data Types/Time Interval
Содержание
- 1 Format interval type column
- 2 Insert value to interval type column
- 3 INTERVAL "0-5" YEAR TO MONTH: Interval of 0 years 5 months
- 4 INTERVAL "11" MONTH: Interval of 11 months
- 5 INTERVAL25:45.123" DAY TO SECOND(2): Invalid interval because the number of digits in the fractional seconds exceeds the specified precision of 2
- 6 INTERVAL25:45.12" DAY(3) TO SECOND(2): Interval of 123 days 2 hours 25 minutes 45.12 seconds; the precision for days is 3 digits and the precision for the fractional seconds is 2 digits
- 7 INTERVAL25:45" DAY(3) TO SECOND: Invalid interval because the number of digits in the days exceeds the specified precision of 3
- 8 INTERVAL "1234" YEAR(3):
- 9 INTERVAL "123" YEAR(3) TO MONTH: Interval of 123 years with a precision of 3 digits
- 10 INTERVAL "1-3" YEAR TO MONTH: Interval of 1 year 3 months
- 11 INTERVAL "14" MONTH: Interval of 14 months (equivalent to 1 year 2 months)
- 12 INTERVAL "-1-5" YEAR TO MONTH: A negative interval of 1 year 5 months
- 13 INTERVAL "1" YEAR: Interval of 1 year
- 14 INTERVAL "25" MINUTE: Interval of 25 minutes
- 15 INTERVAL "2" HOUR: Interval of 2 hours
- 16 INTERVAL "3 2:00:45" DAY TO SECOND: Interval of 3 days 2 hours 0 minutes 45 seconds
- 17 INTERVAL "3 2:25:45" DAY TO SECOND: Interval of 3 days 2 hours 25 minutes 45 seconds
- 18 INTERVAL "-3 2:25:45" DAY TO SECOND: Negative interval of 3 days 2 hours 25 minutes 45 seconds
- 19 INTERVAL "3" DAY: Interval of 3 days
- 20 INTERVAL "45" SECOND: Interval of 45 seconds
- 21 interval type column
- 22 Use TIMESTAMP in insert statement to add value to interval type column
- 23 Using the INTERVAL DAY TO SECOND Type
- 24 Using Time Intervals
Format interval type column
<source lang="sql">
SQL> create table myTable(
2 break_time interval day(1) to second(2) 3 );
Table created. SQL> SQL> SQL> insert into myTable (break_time )
2 values (TIMESTAMP "2001-09-03 12:47:00.000000" - TIMESTAMP "2001-09-03 13:13:00.000000" );
1 row created. SQL> SQL> insert into myTable ( break_time )
2 values ( TIMESTAMP "2001-09-03 13:35:00.000000" - TIMESTAMP "2001-09-03 13:39:00.000000" );
1 row created. SQL> SQL> insert into myTable ( break_time )
2 values (TIMESTAMP "2001-09-03 16:30:00.000000" - TIMESTAMP "2001-09-03 17:00:00.000000" );
1 row created. SQL> SQL> insert into myTable ( break_time )
2 values ( TIMESTAMP "2001-09-03 17:00:00.000000" - TIMESTAMP "2001-09-03 17:30:00.000000" );
1 row created. SQL> SQL> column break_time format a30 SQL> SQL> select * from myTable;
BREAK_TIME
-0 00:26:00.00 -0 00:04:00.00 -0 00:30:00.00 -0 00:30:00.00 4 rows selected. SQL> SQL> drop table myTable; Table dropped.</source>
Insert value to interval type column
<source lang="sql">
SQL> create table myTable(
2 break_time interval day(1) to second(2) 3 );
Table created. SQL> SQL> SQL> insert into myTable (break_time )
2 values (TIMESTAMP "2001-09-03 12:47:00.000000" - TIMESTAMP "2001-09-03 13:13:00.000000" );
1 row created. SQL> SQL> insert into myTable ( break_time )
2 values ( TIMESTAMP "2001-09-03 13:35:00.000000" - TIMESTAMP "2001-09-03 13:39:00.000000" );
1 row created. SQL> SQL> insert into myTable ( break_time )
2 values (TIMESTAMP "2001-09-03 16:30:00.000000" - TIMESTAMP "2001-09-03 17:00:00.000000" );
1 row created. SQL> SQL> insert into myTable ( break_time )
2 values ( TIMESTAMP "2001-09-03 17:00:00.000000" - TIMESTAMP "2001-09-03 17:30:00.000000" );
1 row created. SQL> SQL> column break_time format a30 SQL> SQL> select * from myTable;
BREAK_TIME
-0 00:26:00.00 -0 00:04:00.00 -0 00:30:00.00 -0 00:30:00.00 4 rows selected. SQL> SQL> drop table myTable; Table dropped.</source>
INTERVAL "0-5" YEAR TO MONTH: Interval of 0 years 5 months
<source lang="sql">
SQL> SQL> CREATE TABLE myTable (
2 id INTEGER, 3 duration INTERVAL YEAR(3) TO MONTH 4 );
Table created. SQL> INSERT INTO myTable (id, duration)VALUES (5, INTERVAL "0-5" YEAR TO MONTH); 1 row created. SQL> select * from myTable;
ID DURATION
---------------------------------------------------------------------------
5 +000-05
SQL> SQL> drop table myTable; Table dropped. SQL></source>
INTERVAL "11" MONTH: Interval of 11 months
<source lang="sql">
SQL> SQL> CREATE TABLE myTable (
2 id INTEGER, 3 duration INTERVAL YEAR(3) TO MONTH 4 );
Table created. SQL> SQL> INSERT INTO myTable (id, duration)VALUES (2, INTERVAL "11" MONTH); 1 row created. SQL> SQL> SQL> select * from myTable;
ID DURATION
---------------------------------------------------------------------------
2 +000-11
SQL> SQL> drop table myTable; Table dropped. SQL></source>
INTERVAL25:45.123" DAY TO SECOND(2): Invalid interval because the number of digits in the fractional seconds exceeds the specified precision of 2
<source lang="sql">
SQL> SQL> CREATE TABLE myTable (
2 id INTEGER, 3 duration INTERVAL DAY(3) TO SECOND (4) 4 );
Table created. SQL> INSERT INTO myTable (id, duration)VALUES (6, INTERVAL "123 2:25:45.123" DAY TO SECOND); INSERT INTO myTable (id, duration)VALUES (6, INTERVAL "123 2:25:45.123" DAY TO SECOND)
*
ERROR at line 1: ORA-01873: the leading precision of the interval is too small
SQL> SQL> select * from myTable; no rows selected SQL> SQL> SQL> drop table myTable; Table dropped. SQL> SQL></source>
INTERVAL25:45.12" DAY(3) TO SECOND(2): Interval of 123 days 2 hours 25 minutes 45.12 seconds; the precision for days is 3 digits and the precision for the fractional seconds is 2 digits
<source lang="sql">
SQL> SQL> CREATE TABLE myTable (
2 id INTEGER, 3 duration INTERVAL DAY(3) TO SECOND (4) 4 );
Table created. SQL> INSERT INTO myTable (id, duration)VALUES (6, INTERVAL "3 2:25:45.12" DAY TO SECOND); 1 row created. SQL> SQL> select * from myTable;
ID DURATION
---------------------------------------------------------------------------
6 +003 02:25:45.1200
SQL> SQL> SQL> drop table myTable; Table dropped. SQL></source>
INTERVAL25:45" DAY(3) TO SECOND: Invalid interval because the number of digits in the days exceeds the specified precision of 3
<source lang="sql">
SQL> SQL> CREATE TABLE myTable (
2 id INTEGER, 3 duration INTERVAL DAY(3) TO SECOND (4) 4 );
Table created. SQL> INSERT INTO myTable (id, duration)VALUES (6, INTERVAL "1234 2:25:45" DAY TO SECOND); INSERT INTO myTable (id, duration)VALUES (6, INTERVAL "1234 2:25:45" DAY TO SECOND)
*
ERROR at line 1: ORA-01873: the leading precision of the interval is too small
SQL> SQL> select * from myTable; no rows selected SQL> SQL> SQL> drop table myTable; Table dropped. SQL></source>
INTERVAL "1234" YEAR(3):
Invalid interval: 1234 contains four digits and therefore contains one too many digits allowed by the precision of 3 (which allows up to three digits).
<source lang="sql">
SQL> SQL> CREATE TABLE myTable (
2 id INTEGER, 3 duration INTERVAL YEAR(3) TO MONTH 4 );
Table created. SQL> SQL> INSERT INTO myTable (id, duration)VALUES (6, INTERVAL "1234" YEAR(3)); INSERT INTO myTable (id, duration)VALUES (6, INTERVAL "1234" YEAR(3))
*
ERROR at line 1: ORA-01873: the leading precision of the interval is too small
SQL> SQL> select * from myTable; no rows selected SQL> SQL> drop table myTable; Table dropped. SQL></source>
INTERVAL "123" YEAR(3) TO MONTH: Interval of 123 years with a precision of 3 digits
<source lang="sql">
SQL> SQL> CREATE TABLE myTable (
2 id INTEGER, 3 duration INTERVAL YEAR(3) TO MONTH 4 );
Table created. SQL> INSERT INTO myTable (id, duration)VALUES (6, INTERVAL "123" YEAR(3)); 1 row created. SQL> select * from myTable;
ID DURATION
---------------------------------------------------------------------------
6 +123-00
SQL> SQL> drop table myTable; Table dropped. SQL></source>
INTERVAL "1-3" YEAR TO MONTH: Interval of 1 year 3 months
<source lang="sql">
SQL> SQL> CREATE TABLE myTable (
2 id INTEGER, 3 duration INTERVAL YEAR(3) TO MONTH 4 );
Table created. SQL> INSERT INTO myTable (id, duration)VALUES (4, INTERVAL "1-3" YEAR TO MONTH); 1 row created. SQL> SQL> select * from myTable;
ID DURATION
---------------------------------------------------------------------------
4 +001-03
SQL> SQL> drop table myTable; Table dropped. SQL></source>
INTERVAL "14" MONTH: Interval of 14 months (equivalent to 1 year 2 months)
<source lang="sql">
SQL> SQL> CREATE TABLE myTable (
2 id INTEGER, 3 duration INTERVAL YEAR(3) TO MONTH 4 );
Table created. SQL> SQL> INSERT INTO myTable (id, duration)VALUES (3, INTERVAL "14" MONTH); 1 row created. SQL> SQL> select * from myTable;
ID DURATION
---------------------------------------------------------------------------
3 +001-02
SQL> SQL> drop table myTable; Table dropped. SQL></source>
INTERVAL "-1-5" YEAR TO MONTH: A negative interval of 1 year 5 months
<source lang="sql">
SQL> SQL> CREATE TABLE myTable (
2 id INTEGER, 3 duration INTERVAL YEAR(3) TO MONTH 4 );
Table created. SQL> INSERT INTO myTable (id, duration)VALUES (6, INTERVAL "-1-5" YEAR TO MONTH); 1 row created. SQL> select * from myTable;
ID DURATION
---------------------------------------------------------------------------
6 -001-05
SQL> SQL> drop table myTable; Table dropped. SQL></source>
INTERVAL "1" YEAR: Interval of 1 year
<source lang="sql">
SQL> SQL> CREATE TABLE myTable (
2 id INTEGER, 3 duration INTERVAL YEAR(3) TO MONTH 4 );
Table created. SQL> SQL> INSERT INTO myTable (id, duration)VALUES (1, INTERVAL "1" YEAR); 1 row created. SQL> SQL> select * from myTable;
ID DURATION
---------------------------------------------------------------------------
1 +001-00
SQL> SQL> drop table myTable; Table dropped. SQL></source>
INTERVAL "25" MINUTE: Interval of 25 minutes
<source lang="sql">
SQL> SQL> CREATE TABLE myTable (
2 id INTEGER, 3 duration INTERVAL DAY(3) TO SECOND (4) 4 );
Table created. SQL> INSERT INTO myTable (id, duration)VALUES (3, INTERVAL "25" MINUTE); 1 row created. SQL> SQL> select * from myTable;
ID DURATION
---------------------------------------------------------------------------
3 +000 00:25:00.0000
SQL> SQL> SQL> drop table myTable; Table dropped. SQL></source>
INTERVAL "2" HOUR: Interval of 2 hours
<source lang="sql">
SQL> SQL> CREATE TABLE myTable (
2 id INTEGER, 3 duration INTERVAL DAY(3) TO SECOND (4) 4 );
Table created. SQL> INSERT INTO myTable (id, duration)VALUES (2, INTERVAL "2" HOUR); 1 row created. SQL> SQL> select * from myTable;
ID DURATION
---------------------------------------------------------------------------
2 +000 02:00:00.0000
SQL> SQL> SQL> drop table myTable; Table dropped. SQL></source>
INTERVAL "3 2:00:45" DAY TO SECOND: Interval of 3 days 2 hours 0 minutes 45 seconds
<source lang="sql">
SQL> SQL> CREATE TABLE myTable (
2 id INTEGER, 3 duration INTERVAL DAY(3) TO SECOND (4) 4 );
Table created. SQL> INSERT INTO myTable (id, duration)VALUES (6, INTERVAL "3 2:00:45" DAY TO SECOND); 1 row created. SQL> SQL> select * from myTable;
ID DURATION
---------------------------------------------------------------------------
6 +003 02:00:45.0000
SQL> SQL> SQL> drop table myTable; Table dropped. SQL></source>
INTERVAL "3 2:25:45" DAY TO SECOND: Interval of 3 days 2 hours 25 minutes 45 seconds
<source lang="sql">
SQL> SQL> CREATE TABLE myTable (
2 id INTEGER, 3 duration INTERVAL DAY(3) TO SECOND (4) 4 );
Table created. SQL> INSERT INTO myTable (id, duration)VALUES (6, INTERVAL "3 2:25:45" DAY TO SECOND); 1 row created. SQL> SQL> select * from myTable;
ID DURATION
---------------------------------------------------------------------------
6 +003 02:25:45.0000
SQL> SQL> SQL> drop table myTable; Table dropped. SQL></source>
INTERVAL "-3 2:25:45" DAY TO SECOND: Negative interval of 3 days 2 hours 25 minutes 45 seconds
<source lang="sql">
SQL> SQL> CREATE TABLE myTable (
2 id INTEGER, 3 duration INTERVAL DAY(3) TO SECOND (4) 4 );
Table created. SQL> INSERT INTO myTable (id, duration)VALUES (6, INTERVAL "-3 2:00:45" DAY TO SECOND); 1 row created. SQL> SQL> select * from myTable;
ID DURATION
---------------------------------------------------------------------------
6 -003 02:00:45.0000
SQL> SQL> SQL> drop table myTable; Table dropped. SQL></source>
INTERVAL "3" DAY: Interval of 3 days
<source lang="sql">
SQL> SQL> CREATE TABLE myTable (
2 id INTEGER, 3 duration INTERVAL DAY(3) TO SECOND (4) 4 );
Table created. SQL> INSERT INTO myTable (id, duration)VALUES (1, INTERVAL "3" DAY); 1 row created. SQL> SQL> select * from myTable;
ID DURATION
---------------------------------------------------------------------------
1 +003 00:00:00.0000
SQL> SQL> SQL> drop table myTable; Table dropped. SQL></source>
INTERVAL "45" SECOND: Interval of 45 seconds
<source lang="sql">
SQL> SQL> CREATE TABLE myTable (
2 id INTEGER, 3 duration INTERVAL DAY(3) TO SECOND (4) 4 );
Table created. SQL> INSERT INTO myTable (id, duration)VALUES (4, INTERVAL "45" SECOND); 1 row created. SQL> SQL> select * from myTable;
ID DURATION
---------------------------------------------------------------------------
4 +000 00:00:45.0000
SQL> SQL> SQL> drop table myTable; Table dropped. SQL></source>
interval type column
<source lang="sql">
format interval type column SQL> create table myTable(
2 break_time interval day(1) to second(2) 3 );
Table created. SQL> SQL> SQL> insert into myTable (break_time )
2 values (TIMESTAMP "2001-09-03 12:47:00.000000" - TIMESTAMP "2001-09-03 13:13:00.000000" );
1 row created. SQL> SQL> insert into myTable ( break_time )
2 values ( TIMESTAMP "2001-09-03 13:35:00.000000" - TIMESTAMP "2001-09-03 13:39:00.000000" );
1 row created. SQL> SQL> insert into myTable ( break_time )
2 values (TIMESTAMP "2001-09-03 16:30:00.000000" - TIMESTAMP "2001-09-03 17:00:00.000000" );
1 row created. SQL> SQL> insert into myTable ( break_time )
2 values ( TIMESTAMP "2001-09-03 17:00:00.000000" - TIMESTAMP "2001-09-03 17:30:00.000000" );
1 row created. SQL> SQL> column break_time format a30 SQL> SQL> select * from myTable;
BREAK_TIME
-0 00:26:00.00 -0 00:04:00.00 -0 00:30:00.00 -0 00:30:00.00 4 rows selected. SQL> SQL> drop table myTable; Table dropped.</source>
Use TIMESTAMP in insert statement to add value to interval type column
<source lang="sql">
SQL> create table myTable(
2 break_time interval day(1) to second(2) 3 );
Table created. SQL> SQL> SQL> insert into myTable (break_time )
2 values (TIMESTAMP "2001-09-03 12:47:00.000000" - TIMESTAMP "2001-09-03 13:13:00.000000" );
1 row created. SQL> SQL> insert into myTable ( break_time )
2 values ( TIMESTAMP "2001-09-03 13:35:00.000000" - TIMESTAMP "2001-09-03 13:39:00.000000" );
1 row created. SQL> SQL> insert into myTable ( break_time )
2 values (TIMESTAMP "2001-09-03 16:30:00.000000" - TIMESTAMP "2001-09-03 17:00:00.000000" );
1 row created. SQL> SQL> insert into myTable ( break_time )
2 values ( TIMESTAMP "2001-09-03 17:00:00.000000" - TIMESTAMP "2001-09-03 17:30:00.000000" );
1 row created. SQL> SQL> column break_time format a30 SQL> SQL> select * from myTable;
BREAK_TIME
-0 00:26:00.00 -0 00:04:00.00 -0 00:30:00.00 -0 00:30:00.00 4 rows selected. SQL> SQL> drop table myTable; Table dropped.</source>
Using the INTERVAL DAY TO SECOND Type
INTERVAL DAY TO SECOND type stores time intervals measured in days and seconds.
<source lang="sql">
SQL> SQL> CREATE TABLE myTable (
2 id INTEGER, 3 duration INTERVAL DAY(3) TO SECOND (4) 4 );
Table created. SQL> SQL> SQL> desc myTable;
Name Null? Type ID NUMBER(38) DURATION INTERVAL DAY(3) TO SECOND(4)
SQL> SQL> drop table myTable; Table dropped.</source>
A precision of 3 for the day and a precision of 4 for the fractional seconds of the duration column means up to three digits may be stored for the day of the interval and up to four digits to the right of the decimal point for the fractional seconds.
An INTERVAL DAY TO SECOND literal value:
- INTERVAL "[+|-][d] [h[:m[:s]]]" [DAY[(days_precision)]])
- [TO HOUR | MINUTE | SECOND[(seconds_precision)]]
where
- + or - is an optional indicator that specifies whether the time interval is positive or negative (default is positive).
- d is the number of days for the interval.
- h is the optional number of hours for the interval; if you supply days and hours, you must include TO HOUR in your literal.
- m is the optional number of minutes for the interval; if you supply days and minutes, you must include TO MINUTES in your literal.
- s is the optional number of seconds for the interval; if you supply days and seconds you must include TO SECOND in your literal.
- days_precision is the optional precision for the days (default is 2).
- seconds_precision is the optional precision for the fractional seconds (default is 6).
Quote from:
Oracle Database 10g SQL (Osborne ORACLE Press Series) (Paperback)
# Paperback: 608 pages
# Publisher: McGraw-Hill Osborne Media; 1st edition (February 20, 2004)
# Language: English
# ISBN-10: 0072229810
# ISBN-13: 978-0072229813
Using Time Intervals
Examples of time intervals include
- 2 year 3 months
- 5 months
- C3 days 5 hours 16 minutes
- 1 day 7 hours
- C6 hours
Time Interval Types
- INTERVAL YEAR[(years_precision)] TO MONTH
- Stores a time interval measured in years and months.
- You can specify an optional precision by supplying years_precision.
- years_precision may be an integer from 0 to 9.
- The default precision is 2, which means you can store two digits for the years in your interval.
- You can store a positive or negative time interval.
INTERVAL DAY[(days_precision)]TO SECOND[(seconds_precision)]
- Stores a time interval measured in days and seconds.
- You can specify an optional precision for the days.
- A days_precision integer is from 0 to 9 (default is 2).
- You can also specify an optional precision for the fractional seconds.
- A seconds_precision integer is from 0 to 9 (default is 6).
- You can store a positive or negative time interval.
Using the INTERVAL YEAR TO MONTH Type
INTERVAL YEAR TO MONTH type stores time intervals measured in years and months.
<source lang="sql">
SQL> SQL> CREATE TABLE myTable (
2 id INTEGER, 3 duration INTERVAL YEAR(3) TO MONTH 4 );
Table created. SQL> SQL> SQL> desc myTable;
Name Null? Type ------------------------------------------------------------------------------------------- ID NUMBER(38) DURATION INTERVAL YEAR(3) TO MONTH
SQL> SQL> drop table myTable; Table dropped.</source>
A precision of 3 for the duration column means up to three digits may be stored for the year part of the interval.
An INTERVAL YEAR TO MONTH literal value:
INTERVAL "[+|-][y][-m]" [YEAR[(years_precision)])] [TO MONTH]
where
- + or - is an optional indicator that specifies whether the time interval is positive or negative (default is positive).
- y is the optional number of years for the interval.
- m is the optional number of months for the interval. If you supply years and months, you must include TO MONTH in your literal.
- years_precision is the optional precision for the years (default is 2).
Quote from:
Oracle Database 10g SQL (Osborne ORACLE Press Series) (Paperback)
# Paperback: 608 pages
# Publisher: McGraw-Hill Osborne Media; 1st edition (February 20, 2004)
# Language: English
# ISBN-10: 0072229810
# ISBN-13: 978-0072229813