共計 2909 個字符,預(yù)計需要花費 8 分鐘才能閱讀完成。
本篇文章為大家展示了如何理解 oracle 的 interval 時間格式,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
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] 指定前導(dǎo)和可選的尾隨字段的整型值。如果前導(dǎo)的字段是年份,而尾隨字段是月, 月字段的整數(shù)值范圍為 0 到 11。
precision 指定前導(dǎo)字段的最大位數(shù)。前導(dǎo)字段精度的有效范圍為 0 到 9,其默認(rèn)值為 2。
前導(dǎo)列的限制:如果您指定了一個尾隨字段,那么它就必須小于前導(dǎo)字段。例如,INTERVAL 0-1 月到 YEAR 是無效的。
select sysdate,sysdate + interval 1 year from dual;– 當(dāng)前時間 + 1 年
select sysdate,sysdate+numtoyminterval(1, year) as res from dual;– 當(dāng)前時間 + 1 年
select sysdate,sysdate + interval 1-0 year to month from dual;– 當(dāng)前時間 + 1 年
select sysdate,sysdate + interval 1-1 year to month from dual;– 當(dāng)前時間 + 1 年 1 月
select sysdate,sysdate + interval 100 year(3) from dual;– 當(dāng)前時間 +100 年
select sysdate,sysdate + interval 100-1 year(3) to month from dual;– 當(dāng)前時間 +100 年 1 月
select sysdate,sysdate + interval 100 year(2) from dual;– 報錯 ORA-01873: 間隔的前導(dǎo)精度太小,因為 100 是 3 位數(shù),而 2 只指定了兩位
select sysdate,sysdate + interval 100-1 year(2) to month from dual;– 報錯 ORA-01873: 間隔的前導(dǎo)精度太小,因為 100 是 3 位數(shù),而 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 指定天數(shù)。如果這個值包含的數(shù)字超過了主要精度指定的數(shù)字,那么 Oracle 將返回一個錯誤。
time_expr 指定格式 HH:MI:SS 的時間。
select sysdate,sysdate+1 from dual;– 當(dāng)前時間 + 1 天
select sysdate,sysdate + interval 1 day from dual;– 當(dāng)前時間 + 1 天
select sysdate,sysdate+numtodsinterval(1, day) as res from dual;– 當(dāng)前時間 + 1 天
select sysdate,sysdate + interval 1 0 day to hour from dual;– 當(dāng)前時間 + 1 天
select sysdate,sysdate + interval 1 1 day to hour from dual;– 當(dāng)前時間 + 1 天 1 小時
select sysdate,sysdate + interval 1 01:01 day to minute from dual;– 當(dāng)前時間 + 1 天 1 小時 1 分鐘
select sysdate,sysdate + interval 1 01:01:01 day to second from dual;– 當(dāng)前時間 + 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 秒前的時間點
上述內(nèi)容就是如何理解 oracle 的 interval 時間格式,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注丸趣 TV 行業(yè)資訊頻道。