﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-Roy's Blog-随笔分类-oracle</title><link>http://www.blogjava.net/RoyPayne/category/50518.html</link><description>－敲击思想的键盘，滑动灵感的鼠标。</description><language>zh-cn</language><lastBuildDate>Sat, 31 Dec 2011 06:23:21 GMT</lastBuildDate><pubDate>Sat, 31 Dec 2011 06:23:21 GMT</pubDate><ttl>60</ttl><item><title>ORACLE日期时间函数大全</title><link>http://www.blogjava.net/RoyPayne/archive/2011/12/31/367638.html</link><dc:creator>RoyPayne</dc:creator><author>RoyPayne</author><pubDate>Sat, 31 Dec 2011 05:59:00 GMT</pubDate><guid>http://www.blogjava.net/RoyPayne/archive/2011/12/31/367638.html</guid><wfw:comment>http://www.blogjava.net/RoyPayne/comments/367638.html</wfw:comment><comments>http://www.blogjava.net/RoyPayne/archive/2011/12/31/367638.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/RoyPayne/comments/commentRss/367638.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/RoyPayne/services/trackbacks/367638.html</trackback:ping><description><![CDATA[<div><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">ORACLE日期时间函数大全</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">&nbsp;&nbsp; TO_DATE格式(以时间:2007-11-02&nbsp;&nbsp; 13:45:25为例)<br />&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Year:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; yy two digits 两位年&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 显示值:07<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; yyy three digits 三位年&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 显示值:007<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; yyyy four digits 四位年&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 显示值:2007<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Month:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mm&nbsp;&nbsp;&nbsp; number&nbsp;&nbsp;&nbsp;&nbsp; 两位月&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 显示值:11<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mon&nbsp;&nbsp;&nbsp; abbreviated 字符集表示&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 显示值:11月,若是英文版,显示nov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; month spelled out 字符集表示&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 显示值:11月,若是英文版,显示november&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Day:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dd&nbsp;&nbsp;&nbsp; number&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 当月第几天&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 显示值:02<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ddd&nbsp;&nbsp;&nbsp; number&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 当年第几天&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 显示值:02<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dy&nbsp;&nbsp;&nbsp; abbreviated 当周第几天简写&nbsp;&nbsp;&nbsp; 显示值:星期五,若是英文版,显示fri<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; day&nbsp;&nbsp;&nbsp; spelled out&nbsp;&nbsp; 当周第几天全写&nbsp;&nbsp;&nbsp; 显示值:星期五,若是英文版,显示friday&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ddspth spelled out, ordinal twelfth&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Hour:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hh&nbsp;&nbsp;&nbsp; two digits 12小时进制&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 显示值:01<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hh24 two digits 24小时进制&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 显示值:13<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Minute:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mi&nbsp;&nbsp;&nbsp; two digits 60进制&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 显示值:45<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Second:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ss&nbsp;&nbsp;&nbsp; two digits 60进制&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 显示值:25<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 其它<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Q&nbsp;&nbsp;&nbsp;&nbsp; digit&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 季度&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 显示值:4<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WW&nbsp;&nbsp;&nbsp; digit&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 当年第几周&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 显示值:44<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; W&nbsp;&nbsp;&nbsp; digit&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 当月第几周&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 显示值:1<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 24小时格式下时间范围为： 0:00:00 - 23:59:59....&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 12小时格式下时间范围为： 1:00:00 - 12:59:59 ....&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />1. 日期和字符转换函数用法（to_date,to_char）<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual;&nbsp;&nbsp; //日期转化为字符串&nbsp;&nbsp;&nbsp;<br />select to_char(sysdate,'yyyy') as nowYear&nbsp;&nbsp; from dual;&nbsp;&nbsp; //获取时间的年&nbsp;&nbsp;&nbsp;<br />select to_char(sysdate,'mm')&nbsp;&nbsp;&nbsp; as nowMonth from dual;&nbsp;&nbsp; //获取时间的月&nbsp;&nbsp;&nbsp;<br />select to_char(sysdate,'dd')&nbsp;&nbsp;&nbsp; as nowDay&nbsp;&nbsp;&nbsp; from dual;&nbsp;&nbsp; //获取时间的日&nbsp;&nbsp;&nbsp;<br />select to_char(sysdate,'hh24') as nowHour&nbsp;&nbsp; from dual;&nbsp;&nbsp; //获取时间的时&nbsp;&nbsp;&nbsp;<br />select to_char(sysdate,'mi')&nbsp;&nbsp;&nbsp; as nowMinute from dual;&nbsp;&nbsp; //获取时间的分&nbsp;&nbsp;&nbsp;<br />select to_char(sysdate,'ss')&nbsp;&nbsp;&nbsp; as nowSecond from dual;&nbsp;&nbsp; //获取时间的秒</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">&nbsp;&nbsp;&nbsp;&nbsp;<br />select to_date('2004-05-07 13:23:44','yyyy-mm-dd hh24:mi:ss')&nbsp;&nbsp;&nbsp; from dual//</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">2.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; select to_char( to_date(222,'J'),'Jsp') from dual&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; 显示Two Hundred Twenty-Two&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">3.求某天是星期几&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day') from dual;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; 星期一&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; monday&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; 设置日期语言&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN';&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; 也可以这样&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; TO_DATE ('2002-08-26', 'YYYY-mm-dd', 'NLS_DATE_LANGUAGE = American')&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">4. 两个日期间的天数&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; select floor(sysdate - to_date('20020405','yyyymmdd')) from dual;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">5. 时间为null的用法&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; select id, active_date from table1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; UNION&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; select 1, TO_DATE(null) from dual;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; 注意要用TO_DATE(null)&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">6.月份差&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; a_date between to_date('20011201','yyyymmdd') and to_date('20011231','yyyymmdd')&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; 那么12月31号中午12点之后和12月1号的12点之前是不包含在这个范围之内的。&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; 所以，当时间需要精确的时候，觉得to_char还是必要的&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />7. 日期格式冲突问题&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; 输入的格式要看你安装的ORACLE字符集的类型, 比如: US7ASCII, date格式的类型就是: '01-Jan-01'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; alter system set NLS_DATE_LANGUAGE = American&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; alter session set NLS_DATE_LANGUAGE = American&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; 或者在to_date中写&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; 注意我这只是举了NLS_DATE_LANGUAGE，当然还有很多，&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; 可查看&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; select * from nls_session_parameters&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; select * from V$NLS_PARAMETERS&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">8.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; select count(*)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; from ( select rownum-1 rnum&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; from all_objects&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where rownum &lt;= to_date('2002-02-28','yyyy-mm-dd') - to_date('2002-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 02-01','yyyy-mm-dd')+1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; where to_char( to_date('2002-02-01','yyyy-mm-dd')+rnum-1, 'D' )&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; not in ( '1', '7' )&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; 查找2002-02-28至2002-02-01间除星期一和七的天数&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; 在前后分别调用DBMS_UTILITY.GET_TIME, 让后将结果相减(得到的是1/100秒, 而不是毫秒).&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">9. 查找月份&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; select months_between(to_date('01-31-1999','MM-DD-YYYY'),to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; select months_between(to_date('02-01-1999','MM-DD-YYYY'),to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; 1.03225806451613&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />10. Next_day的用法&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; Next_day(date, day)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; Monday-Sunday, for format code DAY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; Mon-Sun, for format code DY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; 1-7, for format code D&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">11&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; select to_char(sysdate,'hh:mi:ss') TIME from all_objects&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; 注意：第一条记录的TIME 与最后一行是一样的&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; 可以建立一个函数来处理这个问题&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; create or replace function sys_date return date is&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; begin&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; return sysdate;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; end;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; select to_char(sys_date,'hh:mi:ss') from all_objects;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />12.获得小时数&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; extract()找出日期或间隔值的字段值<br />&nbsp;&nbsp;&nbsp; SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 2:38:40') from offer&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; SQL&gt; select sysdate ,to_char(sysdate,'hh') from dual;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; SYSDATE TO_CHAR(SYSDATE,'HH')&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; -------------------- ---------------------&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; 2003-10-13 19:35:21 07&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; SQL&gt; select sysdate ,to_char(sysdate,'hh24') from dual;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; SYSDATE TO_CHAR(SYSDATE,'HH24')&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; -------------------- -----------------------&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; 2003-10-13 19:35:21 19&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />13.年月日的处理&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; select older_date,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newer_date,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; years,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; months,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; abs(&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; trunc(&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newer_date-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; add_months( older_date,years*12+months )&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ) days&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; from ( select&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; trunc(months_between( newer_date, older_date )/12) YEARS,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mod(trunc(months_between( newer_date, older_date )),12 ) MONTHS,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newer_date,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; older_date&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; from (&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select hiredate older_date, add_months(hiredate,rownum)+rownum newer_date&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; from emp&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">14.处理月份天数不定的办法&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; select to_char(add_months(last_day(sysdate) +1, -2), 'yyyymmdd'),last_day(sysdate) from dual&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">16.找出今年的天数&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dual&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">&nbsp;&nbsp; 闰年的处理方法&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; to_char( last_day( to_date('02'&nbsp;&nbsp;&nbsp; | | :year,'mmyyyy') ), 'dd' )&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; 如果是28就不是闰年&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">17.yyyy与rrrr的区别&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; 'YYYY99 TO_C&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; ------- ----&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; yyyy 99 0099&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; rrrr 99 1999&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; yyyy 01 0001&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; rrrr 01 2001&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">18.不同时区的处理&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; select to_char( NEW_TIME( sysdate, 'GMT','EST'), 'dd/mm/yyyy hh:mi:ss') ,sysdate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; from dual;&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">19.5秒钟一个间隔&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; Select TO_DATE(FLOOR(TO_CHAR(sysdate,'SSSSS')/300) * 300,'SSSSS') ,TO_CHAR(sysdate,'SSSSS')&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; from dual&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">&nbsp;&nbsp; 2002-11-1 9:55:00 35786&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; SSSSS表示5位秒数&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">20.一年的第几天&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; select TO_CHAR(SYSDATE,'DDD'),sysdate from dual<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; 310 2002-11-6 10:03:51&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">21.计算小时,分,秒,毫秒&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; select&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; Days,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; A,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; TRUNC(A*24) Hours,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; TRUNC(A*24*60 - 60*TRUNC(A*24)) Minutes,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; TRUNC(A*24*60*60 - 60*TRUNC(A*24*60)) Seconds,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; TRUNC(A*24*60*60*100 - 100*TRUNC(A*24*60*60)) mSeconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; from&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; (&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; select&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; trunc(sysdate) Days,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; sysdate - trunc(sysdate) A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; from dual&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; )&nbsp;&nbsp;&nbsp;&nbsp;</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; "><br /></span><span style="font-size: small; ">&nbsp;&nbsp; select * from tabname&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; order by decode(mode,'FIFO',1,-1)*to_char(rq,'yyyymmddhh24miss');&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; floor((date2-date1) /365) 作为年&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; floor((date2-date1, 365) /30) 作为月&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; d(mod(date2-date1, 365), 30)作为日.</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">23.next_day函数&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 返回下个星期的日期,day为1-7或星期日-星期六,1表示星期日<br />&nbsp;&nbsp; next_day(sysdate,6)是从当前开始下一个星期五。后面的数字是从星期日开始算起。&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; 1 2 3 4 5 6 7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; 日 一 二 三 四 五 六&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; ---------------------------------------------------------------&nbsp;<br />&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; select&nbsp;&nbsp;&nbsp; (sysdate-to_date('2003-12-03 12:55:45','yyyy-mm-dd hh24:mi:ss'))*24*60*60 from ddual<br />&nbsp;&nbsp; 日期 返回的是天 然后 转换为ss<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />24,round[舍入到最接近的日期](day:舍入到最接近的星期日)<br />&nbsp;&nbsp; select sysdate S1,<br />&nbsp;&nbsp; round(sysdate) S2 ,<br />&nbsp;&nbsp; round(sysdate,'year') YEAR,<br />&nbsp;&nbsp; round(sysdate,'month') MONTH ,<br />&nbsp;&nbsp; round(sysdate,'day') DAY from dual</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">25,trunc[截断到最接近的日期,单位为天] ,返回的是日期类型<br />&nbsp;&nbsp; select sysdate S1,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; trunc(sysdate) S2,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //返回当前日期,无时分秒<br />&nbsp;&nbsp;&nbsp;&nbsp; trunc(sysdate,'year') YEAR,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //返回当前年的1月1日,无时分秒<br />&nbsp;&nbsp;&nbsp;&nbsp; trunc(sysdate,'month') MONTH ,&nbsp;&nbsp;&nbsp;&nbsp; //返回当前月的1日,无时分秒<br />&nbsp;&nbsp;&nbsp;&nbsp; trunc(sysdate,'day') DAY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //返回当前星期的星期天,无时分秒<br />&nbsp;&nbsp; from dual</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">26,返回日期列表中最晚日期<br />&nbsp;&nbsp; select greatest('01-1月-04','04-1月-04','10-2月-04') from dual</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">27.计算时间差<br />&nbsp;&nbsp;&nbsp;&nbsp; 注:oracle时间差是以天数为单位,所以换算成年月,日<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))/365) as spanYears from dual&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //时间差-年<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select ceil(moths_between(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))) as spanMonths from dual&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //时间差-月<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))) as spanDays from dual&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //时间差-天<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24) as spanHours from dual&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //时间差-时<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24*60) as spanMinutes from dual&nbsp;&nbsp;&nbsp; //时间差-分<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24*60*60) as spanSeconds from dual //时间差-秒</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">28.更新时间<br />&nbsp;&nbsp;&nbsp;&nbsp; 注:oracle时间加减是以天数为单位,设改变量为n,所以换算成年月,日<br />&nbsp;&nbsp;&nbsp;&nbsp; select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),to_char(sysdate+n*365,'yyyy-mm-dd hh24:mi:ss') as newTime from dual&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //改变时间-年<br />&nbsp;&nbsp;&nbsp;&nbsp; select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),add_months(sysdate,n) as newTime from dual&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //改变时间-月<br />&nbsp;&nbsp;&nbsp;&nbsp; select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),to_char(sysdate+n,'yyyy-mm-dd hh24:mi:ss') as newTime from dual&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //改变时间-日<br />&nbsp;&nbsp;&nbsp;&nbsp; select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),to_char(sysdate+n/24,'yyyy-mm-dd hh24:mi:ss') as newTime from dual&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //改变时间-时<br />&nbsp;&nbsp;&nbsp;&nbsp; select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),to_char(sysdate+n/24/60,'yyyy-mm-dd hh24:mi:ss') as newTime from dual&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //改变时间-分<br />&nbsp;&nbsp;&nbsp;&nbsp; select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),to_char(sysdate+n/24/60/60,'yyyy-mm-dd hh24:mi:ss') as newTime from dual&nbsp;&nbsp; //改变时间-秒</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">29.查找月的第一天,最后一天<br />&nbsp;&nbsp;&nbsp;&nbsp; SELECT Trunc(Trunc(SYSDATE, 'MONTH') - 1, 'MONTH') First_Day_Last_Month,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Trunc(SYSDATE, 'MONTH') - 1 / 86400 Last_Day_Last_Month,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Trunc(SYSDATE, 'MONTH') First_Day_Cur_Month,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LAST_DAY(Trunc(SYSDATE, 'MONTH')) + 1 - 1 / 86400 Last_Day_Cur_Month<br />&nbsp;&nbsp; FROM dual;</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; "><br /></span><span style="font-size: small; ">三. 字符函数(可用于字面字符或数据库列)</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">1，字符串截取<br />&nbsp;&nbsp; select substr('abcdef',1,3) from dual</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">2,查找子串位置<br />&nbsp;&nbsp; select instr('abcfdgfdhd','fd') from dual</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">3,字符串连接<br />&nbsp;&nbsp; select 'HELLO'||'hello world' from dual;</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">4, 1)去掉字符串中的空格<br />&nbsp;&nbsp;&nbsp; select ltrim(' abc') s1,<br />&nbsp;&nbsp;&nbsp; rtrim('zhang ') s2,<br />&nbsp;&nbsp;&nbsp; trim(' zhang ') s3 from dual<br />&nbsp;&nbsp; 2)去掉前导和后缀<br />&nbsp;&nbsp;&nbsp; select trim(leading 9 from 9998767999) s1,<br />&nbsp;&nbsp;&nbsp; trim(trailing 9 from 9998767999) s2,<br />&nbsp;&nbsp;&nbsp; trim(9 from 9998767999) s3 from dual;<br />&nbsp;&nbsp;&nbsp;<br />5,返回字符串首字母的Ascii值<br />&nbsp;&nbsp; select ascii('a') from dual</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">6,返回ascii值对应的字母<br />&nbsp;&nbsp; select chr(97) from dual</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">7,计算字符串长度&nbsp;<br />&nbsp;&nbsp; select length('abcdef') from dual</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">8,initcap（首字母变大写） ,lower(变小写),upper(变大写)<br />&nbsp;&nbsp; select lower('ABC') s1,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; upper('def') s2,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; initcap('efg') s3<br />&nbsp;&nbsp; from dual;</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">9,Replace<br />&nbsp;&nbsp; select replace('abc','b','xy') from dual;</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">10,translate<br />&nbsp;&nbsp; select translate('abc','b','xx') from dual; -- x是1位</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">11,lpad [左添充] rpad [右填充](用于控制输出格式)<br />&nbsp;&nbsp; select lpad('func',15,'=') s1, rpad('func',15,'-') s2 from dual;<br />&nbsp;&nbsp; select lpad(dname,14,'=') from dept;</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">12, decode[实现if ..then 逻辑]&nbsp;&nbsp; 注:第一个是表达式,最后一个是不满足任何一个条件的值<br />&nbsp;&nbsp; select deptno,decode(deptno,10,'1',20,'2',30,'3','其他') from dept;<br />&nbsp;&nbsp; 例:<br />&nbsp;&nbsp; select seed,account_name,decode(seed,111,1000,200,2000,0) from t_userInfo//如果seed为111,则取1000;为200,取2000;其它取0<br />&nbsp;&nbsp; select seed,account_name,decode(sign(seed-111),1,'big seed',-1,'little seed','equal seed') from t_userInfo//如果seed&gt;111,则显示大;为200,则显示小;其它则显</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">示相等</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">13 case[实现switch ..case 逻辑]<br />&nbsp;&nbsp;&nbsp; SELECT CASE X-FIELD&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WHEN X-FIELD &lt; 40 THEN 'X-FIELD 小于 40'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WHEN X-FIELD &lt; 50 THEN 'X-FIELD 小于 50'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WHEN X-FIELD &lt; 60 THEN 'X-FIELD 小于 60'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ELSE 'UNBEKNOWN'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; END<br />&nbsp;&nbsp; FROM DUAL&nbsp;<br />&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; 注:CASE语句在处理类似问题就显得非常灵活。当只是需要匹配少量数值时，用Decode更为简洁。</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">四.数字函数<br />1，取整函数（ceil 向上取整,floor 向下取整）<br />&nbsp;&nbsp; select ceil(66.6) N1,floor(66.6) N2 from dual;</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">2, 取幂(power) 和 求平方根(sqrt)<br />&nbsp;&nbsp; select power(3,2) N1,sqrt(9) N2 from dual;</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">3,求余<br />&nbsp;&nbsp; select mod(9,5) from dual;</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">4,返回固定小数位数 (round:四舍五入，trunc:直接截断)<br />&nbsp;&nbsp; select round(66.667,2) N1,trunc(66.667,2) N2 from dual;</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">5,返回值的符号(正数返回为1,负数为-1)<br />&nbsp;&nbsp; select sign(-32),sign(293) from dual;</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">五.转换函数<br />1，to_char()[将日期和数字类型转换成字符类型]<br />&nbsp;&nbsp; 1) select to_char(sysdate) s1,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; to_char(sysdate,'yyyy-mm-dd') s2,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; to_char(sysdate,'yyyy') s3,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; to_char(sysdate,'yyyy-mm-dd hh12:mi:ss') s4,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; to_char(sysdate, 'hh24:mi:ss') s5,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; to_char(sysdate,'DAY') s6&nbsp;<br />&nbsp;&nbsp;&nbsp; from dual;<br />&nbsp;&nbsp; 2) select sal,to_char(sal,'＄99999') n1,to_char(sal,'＄99,999') n2 from emp</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">2, to_date()[将字符类型转换为日期类型]&nbsp;<br />&nbsp;&nbsp;&nbsp; insert into emp(empno,hiredate) values(8000,to_date('2004-10-10','yyyy-mm-dd'));<br />&nbsp;&nbsp;&nbsp;<br />3, to_number() 转换为数字类型&nbsp;<br />&nbsp;&nbsp;&nbsp; select to_number(to_char(sysdate,'hh12')) from dual; //以数字显示的小时数<br />&nbsp;&nbsp;&nbsp;<br />六.其他函数<br />&nbsp;&nbsp; 1.user:&nbsp;<br />&nbsp;&nbsp;&nbsp; 返回登录的用户名称&nbsp;<br />&nbsp;&nbsp;&nbsp; select user from dual;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; 2.vsize:&nbsp;<br />&nbsp;&nbsp;&nbsp; 返回表达式所需的字节数<br />&nbsp;&nbsp;&nbsp; select vsize('HELLO') from dual;<br />&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; 3.nvl(ex1,ex2): 　<br />&nbsp;&nbsp;&nbsp; ex1值为空则返回ex2,否则返回该值本身ex1（常用）&nbsp;<br />&nbsp;&nbsp;&nbsp; 例：如果雇员没有佣金，将显示0，否则显示佣金&nbsp;<br />&nbsp;&nbsp;&nbsp; select comm,nvl(comm,0) from emp;<br />&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; 4.nullif(ex1,ex2):&nbsp;<br />&nbsp;&nbsp;&nbsp; 值相等返空，否则返回第一个值<br />&nbsp;&nbsp;&nbsp; 例：如果工资和佣金相等，则显示空，否则显示工资<br />&nbsp;&nbsp;&nbsp; select nullif(sal,comm),sal,comm from emp;<br />&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; 5.coalesce: 　<br />&nbsp;&nbsp;&nbsp; 返回列表中第一个非空表达式<br />&nbsp;&nbsp;&nbsp; select comm,sal,coalesce(comm,sal,sal*10) from emp;<br />&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; 6.nvl2(ex1,ex2,ex3) :<br />&nbsp;&nbsp;&nbsp; 如果ex1不为空，显示ex2,否则显示ex3<br />&nbsp;&nbsp;&nbsp; 如：查看有佣金的雇员姓名以及他们的佣金&nbsp;<br />&nbsp;&nbsp;&nbsp; 　　select nvl2(comm,ename,') as HaveCommName,comm from emp;<br />&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;<br />七.分组函数<br />max min avg count sum<br />1，整个结果集是一个组<br />&nbsp;&nbsp; 1) 求部门30 的最高工资，最低工资,平均工资，总人数，有工作的人数，工种数量及工资总和<br />&nbsp;&nbsp;&nbsp;&nbsp; select max(ename),max(sal),&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; min(ename),min(sal),<br />&nbsp;&nbsp;&nbsp;&nbsp; avg(sal),<br />&nbsp;&nbsp;&nbsp;&nbsp; count(*) ,count(job),count(distinct(job)) ,<br />&nbsp;&nbsp;&nbsp;&nbsp; sum(sal) from emp where deptno=30;<br />2, 带group by 和 having 的分组<br />&nbsp;&nbsp; 1)按部门分组求最高工资，最低工资，总人数，有工作的人数，工种数量及工资总和<br />&nbsp;&nbsp;&nbsp; select deptno, max(ename),max(sal),<br />&nbsp;&nbsp;&nbsp; min(ename),min(sal),<br />&nbsp;&nbsp;&nbsp; avg(sal),<br />&nbsp;&nbsp;&nbsp; count(*) ,count(job),count(distinct(job)) ,<br />&nbsp;&nbsp;&nbsp; sum(sal) from emp group by deptno;<br />&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp; 2)部门30的最高工资，最低工资，总人数，有工作的人数，工种数量及工资总和&nbsp;<br />&nbsp;&nbsp;&nbsp; select deptno, max(ename),max(sal),<br />&nbsp;&nbsp;&nbsp; min(ename),min(sal),<br />&nbsp;&nbsp;&nbsp; avg(sal),<br />&nbsp;&nbsp;&nbsp; count(*) ,count(job),count(distinct(job)) ,<br />&nbsp;&nbsp;&nbsp; sum(sal) from emp group by deptno having deptno=30;<br />&nbsp;&nbsp;&nbsp;<br />3, stddev 返回一组值的标准偏差<br />&nbsp;&nbsp;&nbsp; select deptno,stddev(sal) from emp group by deptno;<br />&nbsp;&nbsp;&nbsp; variance 返回一组值的方差差<br />&nbsp;&nbsp;&nbsp; select deptno,variance(sal) from emp group by deptno;</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">4, 带有rollup和cube操作符的Group By<br />&nbsp;&nbsp;&nbsp; rollup 按分组的第一个列进行统计和最后的小计<br />&nbsp;&nbsp;&nbsp; cube 按分组的所有列的进行统计和最后的小计<br />&nbsp;&nbsp;&nbsp; select deptno,job ,sum(sal) from emp group by deptno,job;<br />&nbsp;&nbsp;&nbsp; select deptno,job ,sum(sal) from emp group by rollup(deptno,job);&nbsp;<br />&nbsp;&nbsp;&nbsp; cube 产生组内所有列的统计和最后的小计<br />&nbsp;&nbsp;&nbsp; select deptno,job ,sum(sal) from emp group by cube(deptno,job);</span></p><p style="font-family: Arial; font-size: 14px; line-height: 25px; background-color: #ffffff; "><span style="font-size: small; ">八、临时表<br />&nbsp;&nbsp; 只在会话期间或在事务处理期间存在的表.<br />&nbsp;&nbsp; 临时表在插入数据时，动态分配空间&nbsp;<br />&nbsp;&nbsp; create global temporary table temp_dept<br />&nbsp;&nbsp; (dno number,<br />&nbsp;&nbsp; dname varchar2(10))<br />&nbsp;&nbsp; on commit delete rows;<br />&nbsp;&nbsp; insert into temp_dept values(10,'ABC');<br />&nbsp;&nbsp; commit;<br />&nbsp;&nbsp; select * from temp_dept; --无数据显示,数据自动清除<br />&nbsp;&nbsp; on commit preserve rows:在会话期间表一直可以存在（保留数据）<br />&nbsp;&nbsp; on commit delete rows:事务结束清除数据（在事务结束时自动删除表的数据）</span></p></div><img src ="http://www.blogjava.net/RoyPayne/aggbug/367638.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/RoyPayne/" target="_blank">RoyPayne</a> 2011-12-31 13:59 <a href="http://www.blogjava.net/RoyPayne/archive/2011/12/31/367638.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>