在本教程中,我们将介绍oracle interval
数据类型,并演示如何有效处理区间值。
oracle提供了两种日期时间数据类型:date
和timestamp
用于存储时间点数据。另外,它提供interval
数据类型用于存储一段时间。
有两种类型的interval
:
interval year to month
数据类型使用year
和month
字段存储一段时间。
下面介绍一个interval year to month
:
interval year [(year_precision)] to month
year_precision
代表year
字段中的位数。范围从0
到9
。
year_precision
是可选的。如果省略year_precision
参数,则默认为2
。也就是说,默认情况下,最多可以存储99
年和11
个月的期限,这个期限必须小于100
年。
要指定interval year to month
数据类型的文字值,可以使用以下格式:
interval 'year[-month]' leading (precision) to trailing
leading
和trailing
可以是year
或month
。
以下是参数:
year
和month
是整个区间的leading
和trailing
的整数。 如果leading
是year
,trailing
是month
,那么month
字段的范围从0
到11
。trailing
列必须小于leading
列。 例如,interval '1-2' month to year
是无效的,因为leading
列year
大于month
的leading
字段。leading
字段中的最大数字位数。精度范围为:0~9
,默认值为2
。下表说明了interval year to month
文字的示例:
interval year to month 文字 |
含意 |
---|---|
interval '120-3' year(3) to month |
间隔120年,3个月; 由于前导字段的值大于默认精度(2位),因此必须指定前导字段精度year(3) 。 |
interval '105' year(3) |
间隔105年0个月。 |
interval '500' month(3) |
间隔500个月。 |
interval '9' year |
间隔9年,相当于interval '9-0' year to month |
interval '40' month |
40个月或3年4个月,相当于interval '3-4' year to month |
interval '180' year |
无效的时间间隔,因为’180’有3个数字,它大于默认精度(2) |
首先,创建一个名为candidates
的新表格:
create table candidates (
candidate_id number,
first_name varchar2(50) not null,
last_name varchar2(50) not null,
job_title varchar2(255) not null,
year_of_experience interval year to month,
primary key (candidate_id)
);
在此表中,有一个year_of_experience
列,其数据类型为interval year to month
。
其次,将数据插入candidates
表:
insert into candidates (
first_name,
last_name,
job_title,
year_of_experience
)
values (
'camila',
'kramer',
'scm manager',
interval '10-2' year to month
);
在这个上面这个插入语句中,在year_of_experience
列中插入了一个interval '10-2' year to month
间隔字面值。
insert into candidates (
first_name,
last_name,
job_title,
year_of_experience
)
values (
'keila',
'doyle',
'scm staff',
interval '9' month
);
在这个语句中,在year_of_experience
列中插入了一个为interval '9' month
的间隔字面值。
第三,查询interval year to month
列的数据:
select
*
from
candidates;
执行上面查询语句,得到以下结果 -
interval day to second
是一种按天,小时,分钟和秒钟存储一段时间。
以下显示interval day to second
数据类型的语法:
interval day [(day_precision)] to second [(fractional_seconds_precision)]
在上面这个语法中,
day
字段中的位数。取值范围是0~9
,缺省值是2
。second
字段小数部分的位数。范围从0~9
。如果省略fractional_seconds_precision
,则默认为6
。interval year to second
的字面值形式如下:
interval leading (leading_precision) to trailing(fractional_seconds_precision)
下表显示了interval year to second
文字的一些示例:
interval year to second 字面值 |
含意 |
---|---|
interval '11 10:09:08.555' day to second(3) |
11天,10小时,09分08秒,555/千秒。 |
interval '11 10:09' day to minute |
11天,10小时和09分钟。 |
interval '100 10' day(3) to hour |
100天10个小时。 |
interval '999' day(3) |
999天 |
interval '09:08:07.6666666' hour to second(7) |
9小时08分钟和7.6666666秒。 |
interval '09:30' hour to minute |
9小时30分钟。 |
interval '8' hour |
8小时 |
interval '15:30' minute to second |
15分30秒。 |
interval '30' minute |
30分钟 |
interval '5' day |
5天 |
interval '40' hour |
40小时 |
interval '15' minute |
15分钟 |
interval '250' hour(3) |
250个小时 |
interval '15.6789' second(2,3) |
四舍五入到15.679秒。 由于精度为3,分数秒’6789’被舍入为’679’ |
在本教程中,您已学习如何使用oracle interval
数据类型在表中存储时间段。