溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何理解oracle的interval時(shí)間格式

發(fā)布時(shí)間:2021-11-29 15:52:01 來源:億速云 閱讀:460 作者:柒染 欄目:關(guān)系型數(shù)據(jù)庫

本篇文章為大家展示了如何理解oracle的interval時(shí)間格式,內(nèi)容簡明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

INTERVAL:時(shí)間間隔

INTERVAL YEAR TO MONTH,年份和月份之間必須有-
包含年、月的一段時(shí)間的間隔
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]"指定前導(dǎo)和可選的尾隨字段的整型值。如果前導(dǎo)的字段是年份,而尾隨字段是月,月字段的整數(shù)值范圍為0到11。
"precision"  指定前導(dǎo)字段的最大位數(shù)。前導(dǎo)字段精度的有效范圍為0到9,其默認(rèn)值為2。
前導(dǎo)列的限制:如果您指定了一個(gè)尾隨字段,那么它就必須小于前導(dǎo)字段。例如,INTERVAL '0-1'月到Y(jié)EAR是無效的。

select sysdate,sysdate +  interval '1' year from dual;--當(dāng)前時(shí)間+1年
select sysdate,sysdate+numtoyminterval(1,'year') as res from dual;--當(dāng)前時(shí)間+1年
select sysdate,sysdate +  interval '1-0' year to month from dual;--當(dāng)前時(shí)間+1年
select sysdate,sysdate +  interval '1-1' year to month from dual;--當(dāng)前時(shí)間+1年1月
select sysdate,sysdate +  interval '100' year(3) from dual;--當(dāng)前時(shí)間+100年
select sysdate,sysdate +  interval '100-1' year(3) to month from dual;--當(dāng)前時(shí)間+100年1月

select sysdate,sysdate +  interval '100' year(2) from dual;--報(bào)錯(cuò)ORA-01873: 間隔的前導(dǎo)精度太小,因?yàn)?00是3位數(shù),而2只指定了兩位
select sysdate,sysdate +  interval '100-1' year(2) to month from dual;--報(bào)錯(cuò)ORA-01873: 間隔的前導(dǎo)精度太小,因?yàn)?00是3位數(shù),而2只指定了兩位


INTERVAL DAY TO SECOND
包含天、小時(shí)、分鐘、秒的一段時(shí)間的間隔

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"指定天數(shù)。如果這個(gè)值包含的數(shù)字超過了主要精度指定的數(shù)字,那么Oracle將返回一個(gè)錯(cuò)誤。
"time_expr"指定格式HH:MI:SS的時(shí)間。

select sysdate,sysdate+1 from dual;--當(dāng)前時(shí)間+1天
select sysdate,sysdate +  interval '1' day from dual;--當(dāng)前時(shí)間+1天
select sysdate,sysdate+numtodsinterval(1,'day') as res from dual;--當(dāng)前時(shí)間+1天
select sysdate,sysdate +  interval '1 0' day to hour from dual;--當(dāng)前時(shí)間+1天
select sysdate,sysdate +  interval '1 1' day to hour from dual;--當(dāng)前時(shí)間+1天1小時(shí)
select sysdate,sysdate +  interval '1 01:01' day to minute from dual;--當(dāng)前時(shí)間+1天1小時(shí)1分鐘
select sysdate,sysdate +  interval '1 01:01:01' day to second from dual;--當(dāng)前時(shí)間+1天1小時(shí)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小時(shí)前、1分鐘、1秒前的時(shí)間點(diǎn)

上述內(nèi)容就是如何理解oracle的interval時(shí)間格式,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI