如何理解oracle的interval时间格式

本篇文章为大家展示了如何理解oracle的interval时间格式,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

创新互联技术团队10年来致力于为客户提供成都做网站、网站设计、品牌网站建设网络营销推广、搜索引擎SEO优化等服务。经过多年发展,公司拥有经验丰富的技术团队,先后服务、推广了上1000+网站,包括各类中小企业、企事单位、高校等机构单位。

INTERVAL:时间间隔

INTERVAL YEAR TO MONTH,年份和月份之间必须有-
包含年、月的一段时间的间隔
INTERVAL 'integer [- integer ]' { YEAR | MONTH } [ (precision) ] [ TO { YEAR | MONTH } ]

'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 is MONTH, 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, then it must be less significant than the leading field. For example, INTERVAL '0-1' MONTH TO YEAR is not valid.
"integer [-integer]"指定前导和可选的尾随字段的整型值。如果前导的字段是年份,而尾随字段是月,月字段的整数值范围为0到11。
"precision"  指定前导字段的最大位数。前导字段精度的有效范围为0到9,其默认值为2。
前导列的限制:如果您指定了一个尾随字段,那么它就必须小于前导字段。例如,INTERVAL '0-1'月到YEAR是无效的。

select sysdate,sysdate +  interval '1' year from dual;--当前时间+1年
select sysdate,sysdate+numtoyminterval(1,'year') as res from dual;--当前时间+1年
select sysdate,sysdate +  interval '1-0' year to month from dual;--当前时间+1年
select sysdate,sysdate +  interval '1-1' year to month from dual;--当前时间+1年1月
select sysdate,sysdate +  interval '100' year(3) from dual;--当前时间+100年
select sysdate,sysdate +  interval '100-1' year(3) to month from dual;--当前时间+100年1月

select sysdate,sysdate +  interval '100' year(2) from dual;--报错ORA-01873: 间隔的前导精度太小,因为100是3位数,而2只指定了两位
select sysdate,sysdate +  interval '100-1' year(2) to month from dual;--报错ORA-01873: 间隔的前导精度太小,因为100是3位数,而2只指定了两位


INTERVAL DAY TO SECOND
包含天、小时、分钟、秒的一段时间的间隔

INTERVAL '{ integer | integer time_expr | time_expr }'
{ { DAY | HOUR | MINUTE } [ (leading_precision) ]
| SECOND [ (leading_precision [, fractional_seconds_precision ]) ]
}
[ TO { DAY | HOUR | MINUTE | SECOND [ (fractional_seconds_precision) ] } ]

'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 the fractional_seconds_precision value. You can specify time_expr following an integer and a space only if the leading field is DAY.
"integer"指定天数。如果这个值包含的数字超过了主要精度指定的数字,那么Oracle将返回一个错误。
"time_expr"指定格式HH:MI:SS的时间。

select sysdate,sysdate+1 from dual;--当前时间+1天
select sysdate,sysdate +  interval '1' day from dual;--当前时间+1天
select sysdate,sysdate+numtodsinterval(1,'day') as res from dual;--当前时间+1天
select sysdate,sysdate +  interval '1 0' day to hour from dual;--当前时间+1天
select sysdate,sysdate +  interval '1 1' day to hour from dual;--当前时间+1天1小时
select sysdate,sysdate +  interval '1 01:01' day to minute from dual;--当前时间+1天1小时1分钟
select sysdate,sysdate +  interval '1 01:01:01' day to second from dual;--当前时间+1天1小时1分钟1秒
select sysdate,sysdate -interval '1' year,sysdate -interval '1' month,sysdate -interval '1' day,sysdate -interval '1' HOUR,sysdate -interval '1' MINUTE,sysdate-interval '1' second from dual;
--此刻、1年前、1月前、1天前、1小时前、1分钟、1秒前的时间点

上述内容就是如何理解oracle的interval时间格式,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。

分享标题:如何理解oracle的interval时间格式
转载注明:https://www.cdcxhl.com/article6/ijcoig.html

成都网站建设公司_创新互联,为您提供定制网站网站排名外贸建站用户体验自适应网站网站设计公司

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站建设网站维护公司