About the use of INTERVAL function

About the use of INTERVAL function

1. INTERVAL represents a certain period of time [simple usage]
The format is as follows:
INTERVAL '时间' <year/month/day/hour/minute/second>
example:
select sysdate - INTERVAL '10' YEAR as "10年前",
       sysdate - INTERVAL '10' MONTH as "10个月前",
       sysdate - interval '10' day as "10天前",
       sysdate - interval '10' hour as "10小时前",
       sysdate - interval '10' minute as "10分钟前",
       sysdate - interval '10' second as "10秒钟前",
       sysdate - 10 as "10天前",
       sysdate - 10 / 24 as "10小时前",
       sysdate - 10 / (24 * 60) as "10分钟前",
       sysdate - 10 / (24 * 3600) as "10秒钟前"
  from dual;
expression illustrate
INTERVAL ‘10’ YEAR The time interval is 10 years
INTERVAL ‘10’ MONTH The time interval is 10 months
INTERVAL ‘10’ DAY The time interval is 10 days
INTERVAL ‘10’ HOUR The time interval is 10 hours
INTERVAL ‘10’ HOUR The time interval is 10 hours
INTERVAL ‘10’ MINUTE The time interval is 10 minutes
INTERVAL ‘10’ SECOND The time interval is 10 seconds
2. Advanced usage
Format
INTERVAL '[+|-][y][-m]' [YEAR[( years_precision)])] [TO MONTH]
  • [+] or [-] is an optional indicator indicating whether the interval is positive or negative (default is positive).
  • y is an optional parameter representing the number of years portion of the interval.
  • m is an optional parameter representing the number of months part of the interval. If you specify a number of years and months, you must include TO MONTH in the INTERVAL clause.
  • year_precision is an optional parameter used to specify the precision of year numbers (default is 2).
example
# 返回一个时间段
SQL> select interval '1-3' year to month from dual;

INTERVAL'1-3'YEARTOMONTH
---------------------------------------------------------------------------
+01-03

# 可以进行运算操作
SQL> select sysdate + interval '1-3' year to month from dual;

SYSDATE+INTERVAL'1-
-------------------
2022-11-13 01:23:13

# 此处指定了duration列的精度为3,这就是说可以为该列的年数部分存储3位数字,所以报错提示:间隔的前导精度太小
SQL> select interval '2021' year(3) from dual;
select interval '2021' year(3) from dual
                *
ERROR at line 1:
ORA-01873: the leading precision of the interval is too small




Guess you like

Origin blog.csdn.net/Mr_lqh325/article/details/119658480