Oracle 租户常用的时间类型有 date 和 timestamp、 interval 等,更多时间类型用法,请参考《OceanBase SQL参考(Oracle模式)》。
Oracle 租户常用的取数据库时间函数是 sysdate 和 systimestamp ,二者默认展现格式由参数 NLS_DATE_FORMAT 和 NLS_TIMESTAMP_FORMAT 确定。针对 interval 类型的时间函数有 numtodsinterval , numtoyminterval ,可以在数字和 interval 类型之间转换。
- 示例:格式化时间显示
Oracle 租户调整时间类型显示的格式,可以用 to_char 函数,SQL 如下:
obclient> select sysdate, to_char(sysdate,'YYYY/MM/DD HH24:MI:SS') new_date from dual;
+-----------+---------------------+
| SYSDATE | NEW_DATE |
+-----------+---------------------+
| 01-APR-20 | 2020/04/01 22:17:07 |
+-----------+---------------------+
1 row in set (0.00 sec)
- 示例:提取时间中的年/月/日/时/分/秒
Orale 租户从时间中提取年/月/日/时/分/秒,可以用 to_char 函数,SQL如下:
SELECT sysdate
, to_char(sysdate,'yyyy') d_year
, to_char(sysdate,'mm') d_month
, to_char(sysdate,'dd') d_day
, to_char(sysdate,'hh24') d_hour24
, to_char(sysdate,'mi') d_min
,to_char(sysdate,'ss') d_sec
FROM dual;
+-----------+--------+---------+-------+----------+-------+-------+
| SYSDATE | D_YEAR | D_MONTH | D_DAY | D_HOUR24 | D_MIN | D_SEC |
+-----------+--------+---------+-------+----------+-------+-------+
| 01-APR-20 | 2020 | 04 | 01 | 22 | 17 | 24 |
+-----------+--------+---------+-------+----------+-------+-------+
1 row in set (0.00 sec)
- 示例:时间类型加减
Oracle 租户对时间进行加减,可以直接加减(单位是天),也可以借助函数 NUMTODSINTERVAL 、add_months 函数。
SELECT sysdate
, sysdate+1 t0
, sysdate + numtodsinterval(1,'hour') t1
, sysdate + numtodsinterval(1,'minute') t2
, sysdate + numtodsinterval(1,'second') t3
, add_months(sysdate, 1) t4
, add_months(sysdate, -1) t5
FROM dual\G
*************************** 1. row ***************************
SYSDATE: 2020-03-27 17:59:40
T0: 2020-03-28 17:59:40
T1: 2020-03-27 18:59:40
T2: 2020-03-27 18:00:40
T3: 2020-03-27 17:59:41
T4: 2020-04-27 17:59:40
T5: 2020-02-27 17:59:40
1 row in set (0.01 sec)
obclient>
- 示例:interval 相关的时间函数
select sysdate, systimestamp
,numtoyminterval(100,'YEAR') interval_year
,numtoyminterval(100,'MONTH') interval_mon
,numtodsinterval(100,'DAY') interval_day
,numtodsinterval(100,'HOUR') interval_hour
,numtodsinterval(100,'MINUTE') interval_min
,numtodsinterval(100,'SECOND') interval_sec
from dual \G
*************************** 1. row ***************************
SYSDATE: 2020-04-06 08:34:50
SYSTIMESTAMP: 2020-04-06 08:34:50.434602 +08:00
INTERVAL_YEAR: +000000100-00
INTERVAL_MON: +000000008-04
INTERVAL_DAY: +000000100 00:00:00.000000000
INTERVAL_HOUR: +000000004 04:00:00.000000000
INTERVAL_MIN: +000000000 01:40:00.000000000
INTERVAL_SEC: +000000000 00:01:40.000000000
1 row in set (0.01 sec)