INTERVAL的使用

INTERVAL可以用来做时间的加减法:
SELECT INTERVAL '300' MONTH "25年",
       INTERVAL '54-2' YEAR TO MONTH "54年2个月",
       INTERVAL '11:12:10.1234567' hour TO SECOND "小时到秒"
FROM dual;

语法:
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) ] } ]

示例:

SQL> select date '2010-01-01' + interval '21' year from dual;
DATE'2010-01-01'+INTERVAL'21'Y
------------------------------
2031-01-01
SQL> select date '2010-01-01' + interval '123-2' year(4) to month from dual;
DATE'2010-01-01'+INTERVAL'123-
------------------------------
2133-03-01
SQL> select date '2010-01-01' + interval '100' month from dual;
DATE'2010-01-01'+INTERVAL'100'
------------------------------
2018-05-01
SQL> select date '2010-01-01' + interval '100' day(2) from dual;
select date '2010-01-01' + interval '100' day(2) from dual
ORA-01873: the leading precision of the interval is too small --这里应该是day(3) 因为interval之后的100是三位数,默认是2位数
SQL> select date '2010-01-01' + interval '1 2' day to hour from dual;
DATE'2010-01-01'+INTERVAL'12'D
------------------------------
2010-01-02 02:00:00
SQL> select date '2010-01-01' + interval '1 2:3' day to minute from dual;
DATE'2010-01-01'+INTERVAL'12:3
------------------------------
2010-01-02 02:03:00
SQL> select date '2010-01-01' + interval '1 2:3:4' day to second from dual;
DATE'2010-01-01'+INTERVAL'12:3
------------------------------
2010-01-02 02:03:04
SQL> select date '2010-01-01' + interval '1' hour from dual;
DATE'2010-01-01'+INTERVAL'1'HO
------------------------------
2010-01-01 01:00:00
SQL> select date '2010-01-01' + interval '1:2' hour to minute from dual;
DATE'2010-01-01'+INTERVAL'1:2'
------------------------------
2010-01-01 01:02:00
SQL> select date '2010-01-01' + interval '1:2:3' hour to second from dual;
DATE'2010-01-01'+INTERVAL'1:2:
------------------------------
2010-01-01 01:02:03
SQL> select date '2010-01-01' + interval '1:2' minute to second from dual;
DATE'2010-01-01'+INTERVAL'1:2'
------------------------------
2010-01-01 00:01:02

你可能感兴趣的:(Oracle)