日期:2014-05-16  浏览次数:20439 次

Oracle实用日期函数总结

ORACLE日期时间函数大全

?? TO_DATE格式(以时间:2007-11-02?? 13:45:25为例)
??
???????
Year:?????
??????? yy two digits 两位年??????????????? 显示值:07
??????? yyy three digits 三位年??????????????? 显示值:007
??????? yyyy four digits 四位年??????????????? 显示值:2007
???????????
??????? Month:?????
??????? mm??? number???? 两位月????????????? 显示值:11
??????? mon??? abbreviated 字符集表示????????? 显示值:11月,若是英文版,显示nov????
??????? month spelled out 字符集表示????????? 显示值:11月,若是英文版,显示november
?????????
??????? Day:?????
??????? dd??? number???????? 当月第几天??????? 显示值:02
??????? ddd??? number???????? 当年第几天??????? 显示值:02
??????? dy??? abbreviated 当周第几天简写??? 显示值:星期五,若是英文版,显示fri
??????? day??? spelled out?? 当周第几天全写??? 显示值:星期五,若是英文版,显示friday???????
??????? ddspth spelled out, ordinal twelfth
????????????
????????????? Hour:
????????????? hh??? two digits 12小时进制??????????? 显示值:01
????????????? hh24 two digits 24小时进制??????????? 显示值:13
?????????????
????????????? Minute:
????????????? mi??? two digits 60进制??????????????? 显示值:45
?????????????
????????????? Second:
????????????? ss??? two digits 60进制??????????????? 显示值:25
?????????????
????????????? 其它
????????????? Q???? digit???????? 季度????????????????? 显示值:4
????????????? WW??? digit???????? 当年第几周??????????? 显示值:44
????????????? W??? digit????????? 当月第几周??????????? 显示值:1
?????????????
??????? 24小时格式下时间范围为: 0:00:00 - 23:59:59....?????
??????? 12小时格式下时间范围为: 1:00:00 - 12:59:59 ....
???????????
1. 日期和字符转换函数用法(to_date,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;?? //获取时间的秒

???
select to_date('2004-05-07 13:23:44','yyyy-mm-dd hh24:mi:ss')??? from dual//

2.?????
???
select to_char( to_date(222,'J'),'Jsp') from dual?????
???
??? 显示Two Hundred Twenty-Two????

3.求某天是星期几?????
?? select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day') from dual;?????
?? 星期一?????
??
select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual;?????
?? monday
?????
?? 设置日期语言?????
?? ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN';?????
?? 也可以这样?????
?? TO_DATE ('2002-08-26', 'YYYY-mm-dd', 'NLS_DATE_LANGUAGE = American')????

4. 两个日期间的天数?????
??? select floor(sysdate - to_date('20020405','yyyymmdd')) from dual;????

5. 时间为null的用法?????
?? select id, active_date from table1?????
?? UNION?????
?? select 1, TO_DATE(null) from dual;?????
??
?? 注意要用TO_DATE(null)????

6.月份差??
?? a_date between to_date('20011201','yyyymmdd') and to_date('20011231','yyyymmdd')?????
?? 那么12月31号中午12点之后和12月1号的12点之前是不包含在这个范围之内的。?????
?? 所以,当时间需要精确的时候,觉得to_char还是必要的
?????
7. 日期格式冲突问题?????
??? 输入的格式要看你安装的ORACLE字符集的类型, 比如: US7ASCII, date格式的类型就是: '01-Jan-01'?????
??? alter system set NLS_DATE_LANGUAGE = American?????
??? alter session set NLS_DATE_LANG