日期类型: oracle中常用的类型有两个:DATE和TIMESTAMP. DATE精确到秒。 TIMESTAMP可以保存到秒的小数。
select current_date,current_timestamp from dual;
current_date current_timestamp
2020-12-07 11:30:30. 2020-12-07 11:30:30.089563
-
date:类型相见的到的结果为整型,单位是天。
-
timestamp:类型相减或timestamp与date相减的到的结果类型为interval。
-
timestamp与数值加减后的到的是date类型,损失了精读,如果要保留精度可以改用时间间隔类型(interval)
-
月份加减不能使用interval类型:
~~
列举一些常用的oracle中的时间取值方式,希望对大家有用。
~~
select hiredate,
to_number(to_char(hiredate,'hh24')) 时,
to_number(to_char(hiredate,'mi')) 分,
to_number(to_char(hiredate,'ss')) 秒,
to_number(to_char(hiredate,'dd')) 日,
to_number(to_char(hiredate,'mm')) 月,
to_number(to_char(hiredate,'yyyy')) 年,
to_number(to_char(hiredate,'ddd')) 年内第几天,
trunc(hiredate,'dd') 一天之始,
trunc(hiredate,'day') 周初,
trunc(hiredate,'mm') 月初,
last_day(hiredate) 月末,
add_months(trunc(hiredate,'mm'),1) 下月初,
trunc(hiredate,'yy') 年初,
to_char(hiredate,'day') 周几,
to_char(hiredate,'month') 月份
from (select hiredate+30/24/60/60+20/24/60+5/24/ as hiredate from emp where rownum<=1)