Interval YEAR to MONTH

71. Given below is a list of datetime data types and examples of values stored
in them in a random order:
Datatype Example
1)INTERVAL YEAR TO MONTH a) '20030415 8:00:00 8:00'
2)TIMESTAMP WITH LOCAL TIME ZONE b) '+06 03:30:16.000000'
3)TIMESTAMP WITH TIME ZONE c) '17JUN0312.00.00.000000 AM'
4)INTERVAL DAY TO SECOND d) '+0200'
Identify the option that correctly matches the data types with the values.

A. 1d,2c,3a,4b
B. 1b,2a,3c,4d
C. 1b,2a,3d,4c
D. 1d,2c,3b,4a
Answer: A

72. View the Exhibit and examine the description of the PRODUCT_INFORMATION
table.You want to display the expiration date of the warranty for a product.
Which SQL statement would you execute?


A. SELECT product_id, SYSDATE + warranty_period
FROM product_information;
B. SELECT product_id, TO_YMINTERVAL(warranty_period)
FROM product_information;
C. SELECT product_id, TO_YMINTERVAL(SYSDATE) + warranty_period
FROM product_information;
D. SELECT product_id, TO_YMINTERVAL(SYSDATE + warranty_period)
FROM product_information;
Answer: A

产品的到期时间



Interval Literals

An interval literal specifies a period of time. You can specify these differences in terms of years and months, or in terms of days, hours, minutes, and seconds. Oracle Database supports two types of interval literals, YEAR TO MONTH and DAY TO SECOND. Each type contains a leading field and may contain a trailing field. The leading field defines the basic unit of date or time being measured. The trailing field defines the smallest increment of the basic unit being considered. For example, a YEAR TO MONTH interval considers an interval of years to the nearest month. A DAY TO MINUTE interval considers an interval of days to the nearest minute.

If you have date data in numeric form, then you can use the NUMTOYMINTERVAL or NUMTODSINTERVAL conversion function to convert the numeric data into interval values.

Interval literals are used primarily with analytic functions.

INTERVAL YEAR TO MONTH

Specify YEAR TO MONTH interval literals using the following syntax:

interval_year_to_month::=

Description of interval_year_to_month.gif follows
Description of the illustration interval_year_to_month.gif

where

  • 'integer [-integer]' specifies integer values for the leading and optional trailing field of the literal. If the leading field is YEAR and the trailing field isMONTH, then the range of integer values for the month field is 0 to 11.

  • precision is the maximum number of digits in the leading field. The valid range of the leading field precision is 0 to 9 and its default value is 2.

Restriction on the Leading Field If you specify a trailing field, it must be less significant than the leading field. For example, INTERVAL '0-1MONTH TO YEAR is not valid.

The following INTERVAL YEAR TO MONTH literal indicates an interval of 123 years, 2 months:

INTERVAL '123-2' YEAR(3) TO MONTH

Examples of the other forms of the literal follow, including some abbreviated versions:

Form of Interval LiteralInterpretation
INTERVAL '123-2' YEAR(3) TO MONTHAn interval of 123 years, 2 months. You must specify the leading field precision if it is greater than the default of 2 digits.
INTERVAL '123' YEAR(3)An interval of 123 years 0 months.
INTERVAL '300' MONTH(3)An interval of 300 months.
INTERVAL '4' YEARMaps to INTERVAL '4-0' YEAR TO MONTH and indicates 4 years.
INTERVAL '50' MONTHMaps to INTERVAL '4-2' YEAR TO MONTH and indicates 50 months or 4 years 2 months.
INTERVAL '123' YEARReturns an error, because the default precision is 2, and '123' has 3 digits.

You can add or subtract one INTERVAL YEAR TO MONTH literal to or from another to yield another INTERVAL YEAR TO MONTH literal. For example:

INTERVAL '5-3' YEAR TO MONTH + INTERVAL'20' MONTH = 
INTERVAL '6-11' YEAR TO MONTH

INTERVAL DAY TO SECOND

Specify DAY TO SECOND interval literals using the following syntax:

interval_day_to_second::=

Description of interval_day_to_second.gif follows
Description of the illustration interval_day_to_second.gif

where

  • integer specifies the number of days. If this value contains more digits than the number specified by the leading precision, then Oracle returns an error.

  • time_expr specifies a time in the format HH[:MI[:SS[.n]]] or MI[:SS[.n]] or SS[.n], where n specifies the fractional part of a second. If n contains more digits than the number specified by fractional_seconds_precision, then n is rounded to the number of digits specified by thefractional_seconds_precision value. You can specify time_expr following an integer and a space only if the leading field is DAY.

  • leading_precision is the number of digits in the leading field. Accepted values are 0 to 9. The default is 2.

  • fractional_seconds_precision is the number of digits in the fractional part of the SECOND datetime field. Accepted values are 1 to 9. The default is 6.

Restriction on the Leading Field: If you specify a trailing field, it must be less significant than the leading field. For example, INTERVAL MINUTE TO DAY is not valid. As a result of this restriction, if SECOND is the leading field, the interval literal cannot have any trailing field.

The valid range of values for the trailing field are as follows:

  • HOUR: 0 to 23

  • MINUTE: 0 to 59

  • SECOND: 0 to 59.999999999

Examples of the various forms of INTERVAL DAY TO SECOND literals follow, including some abbreviated versions:

Form of Interval LiteralInterpretation
INTERVAL '4 5:12:10.222' DAY TO SECOND(3)4 days, 5 hours, 12 minutes, 10 seconds, and 222 thousandths of a second.
INTERVAL '4 5:12' DAY TO MINUTE4 days, 5 hours and 12 minutes.
INTERVAL '400 5' DAY(3) TO HOUR400 days 5 hours.
INTERVAL '400' DAY(3)400 days.
INTERVAL '11:12:10.2222222' HOUR TO SECOND(7)11 hours, 12 minutes, and 10.2222222 seconds.
INTERVAL '11:20' HOUR TO MINUTE11 hours and 20 minutes.
INTERVAL '10' HOUR10 hours.
INTERVAL '10:22' MINUTE TO SECOND10 minutes 22 seconds.
INTERVAL '10' MINUTE10 minutes.
INTERVAL '4' DAY4 days.
INTERVAL '25' HOUR25 hours.
INTERVAL '40' MINUTE40 minutes.
INTERVAL '120' HOUR(3)120 hours.
INTERVAL '30.12345' SECOND(2,4)30.1235 seconds. The fractional second '12345' is rounded to '1235' because the precision is 4.

You can add or subtract one DAY TO SECOND interval literal from another DAY TO SECOND literal. For example.

INTERVAL'20' DAY - INTERVAL'240' HOUR = INTERVAL'10-0' DAY TO SECOND

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值