--1.日期字符转换函数的用法
/****************************TO_CHAR********************************/
-----------------------------------------------------日期转化为字符串
SELECT TO_CHAR(sysdate,'YYYY/MM/DD hh24:mi:ss') as NowTime FROM dual;
-----------------------------------------------------获取当前时间的年
SELECT TO_CHAR(sysdate,'YYYY') as NowYear FROM dual;
-----------------------------------------------------获取当前时间的月
SELECT TO_CHAR(sysdate,'MM') as NowMonth FROM dual;
-----------------------------------------------------获取当前时间的日
SELECT TO_CHAR(sysdate,'DD') as NowDay FROM dual;
-----------------------------------------------------获取当前时间的时
SELECT TO_CHAR(sysdate,'hh24') as NowHour FROM dual;
-----------------------------------------------------获取当前时间的分
SELECT TO_CHAR(sysdate,'mi') as NowMinute FROM dual;
-----------------------------------------------------获取当前时间的秒
SELECT TO_CHAR(sysdate,'ss') as NowSecond FROM dual; /****************************TO_DATE********************************/
SELECT TO_DATE('2015/12/30 11:18:54','YYYY/MM/DD hh24:mi:ss') FROM dual; ---1.显示Two Hundred Twenty-Two
select to_char( to_date(222,'J'),'Jsp') from dual --2.求某天是星期几
SELECT TO_CHAR(TO_DATE('2015-08-08','YYYY/MM/DD'),'DAY') FROM dual;
--2.1指定显示语言
select to_char(to_date('2015-08-08','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual; --3.设置日期语言
ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN'; --4.两个日期之间的天数
select floor(sysdate - to_date('2015/11/30','yyyy/mm/dd')) from dual; --5.月份差(scott用户下的emp表)
SELECT * FROM EMP WHERE hiredate between to_date('1985/01/01','YYYY/MM/DD') AND to_date('1988/01/01','YYYY/MM/DD') --6.增加或者减去月份
----------------------6.1增加
SELECT TO_CHAR(ADD_MONTHS(TO_DATE('','YYYYMMDD'),2), 'YYYY-MM-DD') FROM DUAL;
----------------------6.2减去
SELECT TO_CHAR(ADD_MONTHS(SYSDATE, -1), 'YYYY-MM-DD') FROM DUAL --7.返回日期的最后一天
Select LAST_DAY(SYSDATE) FROM dual; --8.两个日期间相差几个月
SELECT MONTHS_BETWEEN(TO_DATE('2011-05-03', 'YYYY-MM-DD'), TO_DATE('2011-01-23', 'YYYY-MM-DD')) FROM DUAL; --9.不同地点的不同时间
SELECT TO_CHAR(SYSDATE, 'YYYY.MM.DD HH24:MI:SS') BeiJing_Time,
TO_CHAR(NEW_TIME(SYSDATE, 'PDT', 'GMT'), 'YYYY.MM.DD HH24:MI:SS') LOS_ANGELS FROM DUAL;
/*
简写 时区
AST OR ADT 大西洋标准时间
HST OR HDT 阿拉斯加—夏威夷时间
BST OR BDT 英国夏令时
MST OR MDT 美国山区时间
CST OR CDT 美国*时区
NST 新大陆标准时间
EST OR EDT 美国东部时间
PST OR PDT 太平洋标准时间
GMT 格伦威治标准时间
YST OR YDT Yukon标准时间
*/ --10.当前会话时区中日期
SELECT CURRENT_DATE FROM DUAL; --11.以timestamp with time zone数据类型返回当前会话时区中的当前日期
SELECT CURRENT_TIMESTAMP FROM DUAL; --12.返回时区
SELECT DBTIMEZONE FROM DUAL; --13.获取日期字符串的间隔
SELECT EXTRACT(MONTH FROM SYSDATE) "MONTH" FROM DUAL; SELECT EXTRACT(DAY FROM SYSDATE) AS "DAY" FROM DUAL; SELECT EXTRACT(YEAR FROM SYSDATE) AS "YEAR" FROM DUAL; --14.上月最后一天
SELECT TO_CHAR(LAST_DAY(ADD_MONTHS(SYSDATE, -1)), 'YYYY/MM/DD') FROM DUAL; --15.上月第一天
SELECT TO_CHAR(ADD_MONTHS(LAST_DAY(SYSDATE)+1,-2),'YYYY-MM-DD') FirstDay FROM DUAL;