Oracle INTERVAL DAY TO SECOND data type INTERVAL DAY TO SECOND Oracle database type of use

Oracle Database INTERVAL DAY TO SECOND type use

INTERVAL DAY TO SECOND type units can be used to store the second time interval days. The following statement creates a table called promotions used to store promotions. promotions table contains a INTERVAL DAY TO SECOND type columns duration, the column for recording promotional effective time interval:

 CREATE TABLE promotions (
promotion_id INTEGER CONSTRAINT promotions_pk PRIMARY KEY,
name VARCHAR2(30) NOT NULL,
duration INTERVAL DAY(3) TO SECOND (4)
);

Note that the accuracy of the fractional part is specified here transit accuracy duration column 3, 4 seconds. That number can be three day storage for the column, the second column for storing up to 4 digits to the right of the decimal point.

To provide a INTERVAL DAY TO SECOND literal database, you can use the following simplified syntax:

INTERVAL '[+|-][ d] [ h[: m[: s]]]' [DAY[( days_precision)]])
[TO HOUR | MINUTE | SECOND[( seconds_precision)]]

among them

● + or - is an optional indicator for explaining the time interval is positive or negative (the default is a positive number).

● d is the number of days interval.

● h is an optional parameter indicating the number of hours interval. If the designated days and hours, must be included in the TO HOUR INTERVAL clause.

● h is an optional parameter indicating the number of minute interval. If you specify days and hours, it must be included in the TO MINUTES INTERVAL clause.

● s is an optional parameter indicating the number of seconds of the time interval. If you specify days and seconds, it must be included in the TO SECOND INTERVAL clause.

● days_precision is an optional parameter for tomorrow with precision number (the default is 2).

● seconds_precision is an optional parameter used to describe the accuracy of seconds (default 6).

TABLE 5-12 gives several examples of type INTERVAL DAY TO SECOND interval literal.

Examples 5-12 Table interval literals

Interval literals

Explanation

INTERVAL ‘3’ DAY

Interval of 3 days

INTERVAL ‘2’ HOUR

Time is 2 hours

INTERVAL ‘25’ MINUTE

The time interval of 25 minutes

INTERVAL ‘45’ SECOND

Interval 45 seconds

INTERVAL ‘3 2’ DAY TO HOUR

Interval to 3 days and 2 hours

INTERVAL ‘3 2:25’ DAY TO MINUTE

Interval to 3 days and 2 hours 25 minutes

INTERVAL ‘3 2:25:45’ DAY TO SECOND

Interval to 3 days and 2 hours 25 minutes 45 seconds

INTERVAL ‘123 2:25:45.12’ DAY(3)

TO SECOND(2)

Interval of 123 days and 2 hours 25 minutes 45.12 seconds; accuracy day 3 digits, the accuracy of fractions of a second digit is 2

INTERVAL ‘3 2:00:45’ DAY TO SECOND

Interval of 3 days 2 hours 0 minutes 45 seconds

INTERVAL ‘-3 2:25:45’ DAY TO SECOND

Interval is negative, a value of 3 days and 2 hours 25 minutes 45 seconds

INTERVAL ‘1234 2:25:45’ DAY(3)

TO SECOND

The time interval is not valid because the median days exceeds the specified accuracy of 3

INTERVAL ‘123 2:25:45.123’ DAY

TO SECOND(2)

Invalid intervals, because the number of fractional digits exceeds the second specified precision 2

The following INSERT statement adds a row to the table promotions:

 INSERT INTO promotions (promotion_id, name, duration)
VALUES (1, '10% off Z Files', INTERVAL '3' DAY);

 

INSERT INTO promotions (promotion_id, name, duration)
VALUES (2, '20% off Pop 3', INTERVAL '2' HOUR);

 

INSERT INTO promotions (promotion_id, name, duration)
VALUES (3, '30% off Modern Science', INTERVAL '25' MINUTE);

 

INSERT INTO promotions (promotion_id, name, duration)
VALUES (4, '20% off Tank War', INTERVAL '45' SECOND);

 

INSERT INTO promotions (promotion_id, name, duration)
VALUES (5, '10% off Chemistry', INTERVAL '3 2:25' DAY TO MINUTE);

 

INSERT INTO promotions (promotion_id, name, duration)
VALUES (6, '20% off Creative Yell', INTERVAL '3 2:25:45' DAY TO SECOND);

 

INSERT INTO promotions (promotion_id, name, duration)
VALUES (7, '15% off My Front Line',
INTERVAL '123 2:25:45.12' DAY(3) TO SECOND(2));

下面这个查询对promotions表进行检索,注意duration列值的格式化:

 SELECT *
FROM promotions;
PROMOTION_ID  NAME                              DURATION
------------ ------------------------------   ------------------
1   10% off Z Files                  +003 00:00:00.0000
2   20% off Pop 3                    +000 02:00:00.0000
3   30% off Modern Science           +000 00:25:00.0000
4   20% off Tank War                 +000 00:00:45.0000
5   10% off Chemistry                +003 02:25:00.0000
6   20% off Creative Yell            +003 02:25:45.0000
7   15% off My Front Line            +123 02:25:45.1200

INTERVAL DAY TO SECOND类型可以用来存储单位为天和秒的时间间隔。下面这条语句创建一个名为promotions的表,用来存储促销信息。promotions表包含了一个INTERVAL DAY TO SECOND类型的列duration,该列用来记录促销有效的时间间隔:

 CREATE TABLE promotions (
promotion_id INTEGER CONSTRAINT promotions_pk PRIMARY KEY,
name VARCHAR2(30) NOT NULL,
duration INTERVAL DAY(3) TO SECOND (4)
);

注意此处指定了duration列中天的精度为3,秒的小数部分精度为4。这就是说可以为该列的天存储3位数字,而为该列的秒最多可以在小数点右边存储4位数字。

要向数据库提供一个INTERVAL DAY TO SECOND字面值,可以使用下面的简化语法:

INTERVAL '[+|-][ d] [ h[: m[: s]]]' [DAY[( days_precision)]])
[TO HOUR | MINUTE | SECOND[( seconds_precision)]]

其中

● + 或 - 是一个可选的指示符,用来说明时间间隔是正数还是负数(默认为正数)。

● d 是时间间隔的天数。

● h 是一个可选参数,表示时间间隔的小时数。如果指定了天和小时,必须在INTERVAL子句中包含TO HOUR。

● h 是一个可选参数,表示时间间隔的分钟数。如果指定了天和分,必须在INTERVAL子句中包含TO MINUTES。

● s 是一个可选参数,表示时间间隔的秒数。如果指定了天和秒,必须在INTERVAL子句中包含TO SECOND。

● days_precision是一个可选参数,用来说明天数的精度(默认值为2)。

● seconds_precision是一个可选参数,用来说明秒的精度(默认值为6)。

表5-12给出了几个INTERVAL DAY TO SECOND类型的时间间隔字面量的例子。

表5-12  时间间隔字面量的例子

时间间隔字面量

说明

INTERVAL ‘3’ DAY

时间间隔为3天

INTERVAL ‘2’ HOUR

时间间隔为2小时

INTERVAL ‘25’ MINUTE

时间间隔为25分钟

INTERVAL ‘45’ SECOND

时间间隔为45秒

INTERVAL ‘3 2’ DAY TO HOUR

时间间隔为3天零2小时

INTERVAL ‘3 2:25’ DAY TO MINUTE

时间间隔为3天零2小时25分

INTERVAL ‘3 2:25:45’ DAY TO SECOND

时间间隔为3天零2小时25分45秒

INTERVAL ‘123 2:25:45.12’ DAY(3)

TO SECOND(2)

时间间隔为123天零2小时25分45.12秒; 天的精度是3位数字,秒的小数部分的精度是2位数字

INTERVAL ‘3 2:00:45’ DAY TO SECOND

时间间隔为3天2小时0分45秒

INTERVAL ‘-3 2:25:45’ DAY TO SECOND

时间间隔为负数,值为3天零2小时25分45秒

INTERVAL ‘1234 2:25:45’ DAY(3)

TO SECOND

时间间隔无效,因为天的位数超过了指定的精度3

INTERVAL ‘123 2:25:45.123’ DAY

TO SECOND(2)

时间间隔无效,因为秒的小数部分的位数超过了指定的精度2

下面这个INSERT语句向promotions表添加一行记录:

 INSERT INTO promotions (promotion_id, name, duration)
VALUES (1, '10% off Z Files', INTERVAL '3' DAY);

 

INSERT INTO promotions (promotion_id, name, duration)
VALUES (2, '20% off Pop 3', INTERVAL '2' HOUR);

 

INSERT INTO promotions (promotion_id, name, duration)
VALUES (3, '30% off Modern Science', INTERVAL '25' MINUTE);

 

INSERT INTO promotions (promotion_id, name, duration)
VALUES (4, '20% off Tank War', INTERVAL '45' SECOND);

 

INSERT INTO promotions (promotion_id, name, duration)
VALUES (5, '10% off Chemistry', INTERVAL '3 2:25' DAY TO MINUTE);

 

INSERT INTO promotions (promotion_id, name, duration)
VALUES (6, '20% off Creative Yell', INTERVAL '3 2:25:45' DAY TO SECOND);

 

INSERT INTO promotions (promotion_id, name, duration)
VALUES (7, '15% off My Front Line',
INTERVAL '123 2:25:45.12' DAY(3) TO SECOND(2));

下面这个查询对promotions表进行检索,注意duration列值的格式化:

 SELECT *
FROM promotions;
PROMOTION_ID  NAME                              DURATION
------------ ------------------------------   ------------------
1   10% off Z Files                  +003 00:00:00.0000
2   20% off Pop 3                    +000 02:00:00.0000
3   30% off Modern Science           +000 00:25:00.0000
4   20% off Tank War                 +000 00:00:45.0000
5   10% off Chemistry                +003 02:25:00.0000
6   20% off Creative Yell            +003 02:25:45.0000
7   15% off My Front Line            +123 02:25:45.1200

Guess you like

Origin www.cnblogs.com/Jeely/p/11200019.html