日期函数
1、ADD_MONTHS(date,integer) 给一个日期加上几个月,返回日期
select ADD_MONTHS(sysdate,2) a1,ADD_MONTHS(sysdate,-2) a2from dual;
2、CURRENT_DATE 返回当前Session中,当前时区的时间
select current_date from dual;
3、CURRENT_TIMESTAMP返回当前Session中,当前时区的时间戳
select CURRENT_TIMESTAMPfrom dual;
4、DBTIMEZONE 返回数据库时区
select DBTIMEZONE from dual;
5、EXTRACT(year fromexpr)
EXTRACT(month from expr)
EXTRACT(day from expr)
EXTRACT(hour from expr)
EXTRACT(minute from expr)
EXTRACT(second from expr)
EXTRACT(timezone_hour from expr)
EXTRACT(timezone_hour from expr)
EXTRACT(timezone_minute from expr)
EXTRACT(timezone_region from expr)
EXTRACT(timezone_abbr from expr)
select EXTRACT(year from sysdate) year from dual;
select EXTRACT(month from sysdate) month from dual;
select EXTRACT(day from sysdate) day from dual;
select EXTRACT(HOUR from systimestamp) h from dual;
select EXTRACT(minute from systimestamp) m from dual;
select EXTRACT(second from systimestamp)ssfrom dual;
select EXTRACT(timezone_hour from systimestamp) th from dual;
select EXTRACT(timezone_minute from systimestamp) tm from dual;
select EXTRACT(timezone_region from systimestamp) tg from dual;
select EXTRACT(timezone_abbr from systimestamp) tabbr from dual;
6、FROM_TZ(timaezone_stamp,timezone_value)转换时间戳和时区值为一个带时区的时间戳
SELECT FROM_TZ(TIMESTAMP'2000-03-2808:00:00','3:00') a1FROM DUAL;
7、LAST_DAY(date) 返回包含当前日期所在月份的最后一天
select LAST_DAY(sysdate)a1,LAST_DAY(ADD_MONTHS(sysdate,2)) a2 from dual;
8、LOCALTIMESTAMP(timestamp_precision)返回当前的日期和时间在会话时区时间戳数据类型的值
SELECT CURRENT_TIMESTAMPa1,LOCALTIMESTAMP a2FROM DUAL;
9、MONTHS_BETWEEN(date1,date2)返回date1-date2所相差的月份数[可能是小数,按照每月31天计算]
SELECTMONTHS_BETWEEN(TO_DATE('2013-02-15','YYYY-MM-dd'),TO_DATE('2010-12-1','YYYY-MM-dd') ) a1 FROM DUAL;
10、NEW_TIME(date,timezone1,timezone2)计算当时区timezone1中的日期和时间是date时候,返回时区timezone2中的日期和时间
SELECT NEW_TIME(TO_DATE('11-10-201001:23:45','MM-DD-YYYYHH24:MI:SS'),'AST','PST') a1FROM DUAL;
11、NEXT_DAY(date,ch) 返回时间date的下一个星期几的日期[1-7,星期日表示1]
select NEXT_DAY(sysdate,3) a1 from dual;
12、NUMTODSINTERVAL(n,interval_unit) 将数字与单位转换为内部时间格式, interval_unit格式有:DAY,HOUR,MINUTE,SECOND 【DS表示 DAY——>SECOND】
select sysdate+NUMTODSINTERVAL(10,'DAY')a1,sysdate+NUMTODSINTERVAL(10,'HOUR') a2,sysdate+NUMTODSINTERVAL(10,'MINUTE')a3,sysdate+NUMTODSINTERVAL(10,'SECOND') a4 from dual
13、NUMTOYMINTERVAL(n, interval_unit) 将数字与单位转换为内部时间格式, interval_unit格式有:YEAR ,MONTH【YM表示 YEAR——>MONTH】
select sysdate+NUMTOYMINTERVAL(10,'YEAR') a1,sysdate+NUMTOYMINTERVAL(10,'MONTH')a2from dual
14、ORA_DST_AFFECTED(datetime_expr)
15、ORA_DST_CONVERT(datetime_expr,integer,integer)
16、ORA_DST_ERROR(datetime_expr)
17、ROUND(date,fmt) 日期的四舍五入
SELECT ROUND(sysdate,'YEAR') a1,ROUND(sysdate,'MONTH') a2,ROUND(sysdate,'DAY') a3 FROM DUAL;
18、SESSIONTIMEZONE返回当前session的时区
select SESSIONTIMEZONE a1from dual;
19、SYS_EXTRACT_UTC(datetime_wuth_timezone) 返回UTC时间
SELECT SYS_EXTRACT_UTC(systimestamp) a1FROM DUAL;
20、SYSDATE 返回当前时间,数据库驻留的操作系统时间
select SYSDATE a1from dual;
21、SYSTIMESTAMP 返回系统时间,包含时区信息
select SYSTIMESTAMP a2from dual;
22、TO_CHAR (datetime,fmt)格式化日期为规定文本
select to_char(SYSTIMESTAMP,'yyyy-MM-ddhh24:mi:ss') a1 from dual;
23、TO_DSINTERVAL(fmt) 转换一个格式字符串为一个内部的 DAY——>SECOND
select sysdate+TO_DSINTERVAL('10 00:00:00') a1 from dual;--加10天
select sysdate+TO_DSINTERVAL(to_char(sysdate,'DD hh24:mi:ss')) a1 from dual;--加上当前时间的时分秒
24、TO_YMINTERVAL(fmt)转换一个格式字符串为一个内部的 YEAR——>MONTH
select sysdate+TO_YMINTERVAL('01-03') a1 from dual;--加15个月
select sysdate+TO_YMINTERVAL(to_char(sysdate,'yyyy-MM')) a1 from dual;
25、TO_TIMESTAMP(ch,fmt,nlspara)转换日期字符串为时间戳类型
select TO_TIMESTAMP(sysdate) a1from dual;
select TO_TIMESTAMP('2013-08-1212:11:11','YYYY-MM_DDHH24:MI:SS') a1 from dual;
26、TO_TIMESTAMP_TZ(ch,fmt,nlspara) 转换日期字符串为带时区的时间戳类型
select TO_TIMESTAMP_TZ(systimestamp) a1from dual;
SELECT TO_TIMESTAMP_TZ('1999-12-01 11:00:00-8:00','YYYY-MM-DD HH:MI:SSTZH:TZM') a1 FROM DUAL;
27、TRUNC (date,fmt) 按格式阶段日期
select TRUNC(sysdate,'YEAR') a1 from dual;--当年的一月一日
select TRUNC(sysdate,'MONTH') a1 from dual;--当月的第一天
select TRUNC(sysdate,'DAY') a1 from dual;--当天0时0分0秒
28、TZ_OFFSET(timezone_name),TZ_OFFSET(‘+HH:MI’) ,TZ_OFFSET(‘-HH:MI’), TZ_OFFSET(SESSIONTIMEZONE),TZ_OFFSET(DBTIMEZONE) 返回时区偏移量
select TZ_OFFSET('US/Eastern') a1from dual;
select TZ_OFFSET('+08:00') a1from dual;
select TZ_OFFSET(DBTIMEZONE) a1from dual;