很實(shí)用的時(shí)間處理知識(shí)點(diǎn),。
DI2rkD#Dj\f24513607 [,w*U)S+G1NO24513607舉例說(shuō)明: U!w2Wb+mBc245136071、日期字符轉(zhuǎn)換函數(shù)to_date(),to_char() J7Y"Mp8g`/L X24513607 SQL:select to_date('20080229132545','yyyy-mm-dd hh24:mi:ss') from dual ,; RESULT:2008-2-29 13:25:45 RXx},GR+ZP?x(G24513607 SQL:select to_char(sysdate,'hh:mi:ss') TIME from dual; RESULT:10:51:43 %i\*qh"t^fs245136072、ITPUB個(gè)人空間 eIY7j9B~ SQL:select to_char( to_date(2008,'J'),'Jsp') from dual,; RESULT:Two Thousand Eight 5hc$J"I I0h*h!@[v245136073,、查看哪天的年份 i*J-Q{-W7Q)|(M+H24513607 SQL:select to_char(to_date('2008-2-29','yyyy-mm-dd'),'year') from dual; RESULT:two thousand eightITPUB個(gè)人空間Q)]`rzU%dbqNUP SQL:select to_char(to_date('2008-2-29','yyyy-mm-dd'),'yyyy') from dual; RESULT:2008ITPUB個(gè)人空間j9V2xJo*Z*Tb4H U 4、查看哪天的月份ITPUB個(gè)人空間?K%GO*YR3o,` SQL:select to_char(to_date('2008-2-29','yyyy-mm-dd'),'month') from dual,;RESULT:2月ITPUB個(gè)人空間;i%o^8iE}#g_ SQL:select to_char(to_date('2008-2-29','yyyy-mm-dd'),'mm') from dual,; RESULT:02ITPUB個(gè)人空間&o-QVq'L 5,、查看哪天是星期幾ITPUB個(gè)人空間N0`9IK p SQL:select to_char(to_date('2008-2-29','yyyy-mm-dd'),'day') from dual; RESULT:星期五ITPUB個(gè)人空間E%r'A0h,f ^ D+S SQL:select to_char(to_date('2008-02-29','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual;ITPUB個(gè)人空間#T,WXj$@ ]4X-g RESULT:friday j$o-AfC&}24513607 SQL:select to_char(to_date('2008-2-29','yyyy-mm-dd'),'dd') from dual;RESULT:29 I.x J-G,uK E0L'X#u3S245136076,、查看兩個(gè)日期之間的天數(shù) 1NDM!s4L,i[ E24513607 SQL:select floor(sysdate - to_date('20080201','yyyymmdd')) from dual; RESULT:28ITPUB個(gè)人空間LR$mo$`3[w l 注:sysdate是2008-02-29ITPUB個(gè)人空間,jlp@M_SAwO0z 7,、取兩個(gè)日期之間除了周六周日的天數(shù)ITPUB個(gè)人空間M+{l5c\Ds*P SQL:ITPUB個(gè)人空間a^L9v/@I select count(*)ITPUB個(gè)人空間"g\6Q%iT4rUk from ( select rownum-1 rnumITPUB個(gè)人空間|}+^&G{N/F1\/c?$[yh from all_objects MGPVS$K%w24513607 where rownum <= to_date('2008-02-29','yyyy-mm-dd') - to_date('2008- (vH4UVPM24513607 02-01','yyyy-mm-dd')+1ITPUB個(gè)人空間G/bWz Z7_z'fV E )ITPUB個(gè)人空間2_&sl9a.ban,L } where to_char( to_date('2008-02-01','yyyy-mm-dd')+rnum-1, 'D' )ITPUB個(gè)人空間)vD&r&a!y~%qM notITPUB個(gè)人空間:| Ajy(F:q in ( '6', '7' ) 0T'Tw-s^ ] k.Y24513607 RESULT:ITPUB個(gè)人空間~1ca(Z"N,@ W'[g+d 21 Jt1a ~r#d]8E udo245136078、查看兩個(gè)日期之間的月數(shù) G*D"LYl6k7` E24513607 SQL:select months_between(to_date('2-29-2008', 'MM-DD-YYYY'),to_date('10-1-2007', 'MM-DD-YYYY')) "MONTHS" FROM DUAL;ITPUB個(gè)人空間 `1Q+iz1F1L/? RESULT:4.90322580645161ITPUB個(gè)人空間 ^Q [B.z\!K P K,T%z 9,、next_day函數(shù)ITPUB個(gè)人空間8H+W/z,S#u-BCX next_day(sysdate,6)是從當(dāng)前開(kāi)始下一個(gè)星期五,。后面的數(shù)字是從星期日開(kāi)始算起。 [!ty@$qlk,y24513607 1 2 3 4 5 6 7 2x7{k_;^] }+|6T24513607 日 一 二 三 四 五 六ITPUB個(gè)人空間.^a,l0R i;N+pY 10,、查看小時(shí)分秒 1u2i#I8ae)k5k24513607 select sysdate ,to_char(sysdate,'hh') from dual; *\/]C1|{;Wt$hYQ24513607 select sysdate ,to_char(sysdate,'mi') from dual; y,F;^bA)V*a24513607 select sysdate ,to_char(sysdate,'ss') from dual;ITPUB個(gè)人空間1b E`0^ }X!A 11,、取一年的天數(shù)(例如今年2008) "i[3WY#v$ba24513607 SQL:select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dual ;RESULT:366 lb4X_NZ2451360712,、 yyyy與rrrr的區(qū)別ITPUB個(gè)人空間n+J5B4k%t*j7q+W yyyy 99 0099 7OV'A1{G!emM24513607 rrrr 99 1999ITPUB個(gè)人空間F4v8js m X%QF yyyy 01 0001ITPUB個(gè)人空間 rDAE!bEzNgq rrrr 01 2001 V9b Du2\u)sn8f7q7Y$`2451360713,、一年的第幾天ITPUB個(gè)人空間w^,a~0mO(a2n~O SQL:select TO_CHAR(SYSDATE,'DDD'),sysdate from dual ;RESULT:060ITPUB個(gè)人空間`o|}#X!oA7H 注:sysdate是2008-02-29ITPUB個(gè)人空間gE? F4wZ;Xb7X 14,、add_months()用于從一個(gè)日期值增加或減少一些月份ITPUB個(gè)人空間LT#z6i e t SQL:select add_months(sysdate,12) from dual; RESULT:2009-2-28 11:23:50 ,R$A!G"eZU,Sc%`G v2451360715,、extract()找出日期或間隔值的字段值 ^#bJn T cR24513607 SQL:select extract(month from sysdate) from dual; RESULT:2 0i7HW[!]_*W*K?v24513607 SQL:select extract(year from add_months(sysdate,36)) from dual; RESULT:2011ITPUB個(gè)人空間Pa't&u P?'}| 16、last_day()返回包含了日期參數(shù)的月份的最后一天的日期 rCGH1l1z)cO!I24513607 SQL:select last_day(sysdate) from dual; RESULT:2008-2-29ITPUB個(gè)人空間5g#nrxy |9Ve P[1J5e9d"~{NS24513607 ITPUB個(gè)人空間.HN]H.Qe|4N ITPUB個(gè)人空間/zM)RP;wn"M~T ===================================================================================================================ITPUB個(gè)人空間BY.L0X*Z~KU#\? 6O3Y8~P/o~bc24513607TO_DATE格式 OQl,@RYl#cj24513607 Day: _gIhi'An0fP24513607 dd number 12 f&x+W'X#[\_24513607 dy abbreviated fri (MV.ev*] _\+t24513607 day spelled out friday ITPUB個(gè)人空間3c9R6\a$s ddspth spelled out, ordinal twelfthITPUB個(gè)人空間9Owi^7}1}zu d Month: ITPUB個(gè)人空間qy-m} mt+}+v mm number 03ITPUB個(gè)人空間/tQ4qgKop'ba ~ mon abbreviated marITPUB個(gè)人空間4q,kOC s G,pt Z nF month spelled out march "H {5Y W'M2N |O24513607 Year: N1e.N"Wa3uYxSy24513607 yy two digits 98ITPUB個(gè)人空間?iw1P'C9?Ye b yyyy four digits 1998 ITPUB個(gè)人空間%CM.nl#OR4Q +Fl:G-R NU-c24513607 24小時(shí)格式下時(shí)間范圍為: 0:00:00 - 23:59:59....ITPUB個(gè)人空間Jv)TG^y'd@ 12小時(shí)格式下時(shí)間范圍為: 1:00:00 - 12:59:59 .... 5CQ D5dc1Nl9}8}245136071. )J&@o v/kXF;T24513607 日期和字符轉(zhuǎn)換函數(shù)用法(to_date,to_char) )BN8p+v%x4i?24513607 ITPUB個(gè)人空間 Xd'GA+r*Y 2.ITPUB個(gè)人空間R"n0S@]j H select to_char( to_date(222,'J'),'Jsp') from dual +^w3N*f6kP24513607 9P%zjUSn3[On4d8h24513607 顯示Two Hundred Twenty-Two -^Jj9?"wnK`}24513607 ({3rz9_aw/Z&U0J f v `245136073.求某天是星期幾ITPUB個(gè)人空間uNE`3EVA![ select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day') from dual; ITPUB個(gè)人空間W@[so%d 星期一 ~S#u'AP.b*D0RdA24513607 select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual; ITPUB個(gè)人空間"p_yYp){f'Z4L monday ?a_/^0xvl(U24513607 設(shè)置日期語(yǔ)言ITPUB個(gè)人空間8V~FY,RB-d[ ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN'; !P6y*k&q%Y!]ew1dU24513607 也可以這樣 kM#K7VSh uF24513607 TO_DATE ('2002-08-26', 'YYYY-mm-dd', 'NLS_DATE_LANGUAGE = American') 2t5i2`(aH24513607 nx%B/Wm~ N245136074.兩個(gè)日期間的天數(shù)ITPUB個(gè)人空間wMp!Zv select floor(sysdate - to_date('20020405','yyyymmdd')) from dual;ITPUB個(gè)人空間5H?j g_?&| ITPUB個(gè)人空間0]0u@"o-G 5. 時(shí)間為null的用法 YQ'@1N?{YcT24513607 select id, active_date from table1ITPUB個(gè)人空間P&qA\ ]J0t UNIONITPUB個(gè)人空間I {d"l1J select 1, TO_DATE(null) from dual;ITPUB個(gè)人空間/R!PI1CTul3v"xh ITPUB個(gè)人空間,~w2gqJ:W}/Hc? 注意要用TO_DATE(null) :o.J| u$} _a$y6e\x24513607 ITPUB個(gè)人空間P Y `%sa#O 6. |l:Y"q%P1]/WK24513607 a_date between to_date('20011201','yyyymmdd') and to_date('20011231','yyyymmdd') 1Mr _'c%iIf24513607 那么12月31號(hào)中午12點(diǎn)之后和12月1號(hào)的12點(diǎn)之前是不包含在這個(gè)范圍之內(nèi)的,。ITPUB個(gè)人空間QWyZt:X"O@5R 所以,,當(dāng)時(shí)間需要精確的時(shí)候,覺(jué)得to_char還是必要的 9L s2vhi245136077. 日期格式?jīng)_突問(wèn)題ITPUB個(gè)人空間G ~_'Sll7n(V3^)O2B ~ 輸入的格式要看你安裝的ORACLE字符集的類型, 比如: US7ASCII, date格式的類型就是: '01-Jan-01'ITPUB個(gè)人空間Pg'W)g/Im alter system set NLS_DATE_LANGUAGE = AmericanITPUB個(gè)人空間Y[2R!o1_7|4d{ alter session set NLS_DATE_LANGUAGE = AmericanITPUB個(gè)人空間#]o2_X3e`+e G:WO 或者在to_date中寫(xiě)ITPUB個(gè)人空間(Bp]Uh.V*PQ select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual; p1G0HB,{1x+y${24513607 注意我這只是舉了NLS_DATE_LANGUAGE,,當(dāng)然還有很多,, 5j'oqX9IB24513607 可查看ITPUB個(gè)人空間6q(v&Kd|g [s*O select * from nls_session_parametersITPUB個(gè)人空間9FrB6~\*v select * from V$NLS_PARAMETERSITPUB個(gè)人空間(~OQ hT.oB )B}6dN4bVK24513607 日期和字符轉(zhuǎn)換函數(shù)用法(to_date,to_char) 'O ne kqVKAS\24513607 We overwrite NLS_DATE_FORMAT into different formats for the session. ITPUB個(gè)人空間$F lr8WX.sFq8^z sql> alter session set nls_date_format = 'DD-MON-RR'; Session altered. j%z[f+y ~M24513607 sql> set pagesize 0ITPUB個(gè)人空間8s1A8d c8@(C|U-u m sql> set linesize 130 I5M_F[!p24513607 sql> select * from nls_session_parameters; Yp@0q'U"v!?f24513607 NLS_LANGUAGE AMERICANITPUB個(gè)人空間J(yb'B`"A1X h NLS_TERRITORY AMERICAITPUB個(gè)人空間%o"i ]|D(w&l.R-e NLS_CURRENCY $ITPUB個(gè)人空間`L9\Bb NLS_ISO_CURRENCY AMERICAITPUB個(gè)人空間&X/T$XX]/{u{ NLS_NUMERIC_CHARACTERS ., IB?L&EkF o4Y6j/{24513607 NLS_CALENDAR GREGORIAN Q%\8mbS24513607 NLS_DATE_FORMAT DD-MON-RRITPUB個(gè)人空間Se#l$E(L$qh7H4x NLS_DATE_LANGUAGE AMERICAN O\.?I ek5f W24513607 NLS_SORT BINARY 1Z!_"jB|24513607 NLS_TIME_FORMAT HH.MI.SSXFF AMITPUB個(gè)人空間%u/^#p5jB3C9X)? NLS_TIMESTAMP_FORMAT DD-MON-RR HH.MI.SSXFF AM 0G'^P dat8S24513607 NLS_TIME_TZ_FORMAT HH.MI.SSXFF AM TZH:TZM @o"XE k6`~1e24513607 NLS_TIMESTAMP_TZ_FORMAT DD-MON-RR HH.MI.SSXFF AM TZH:TZMITPUB個(gè)人空間q|;M-R`l.oY+RM NLS_DUAL_CURRENCY $ITPUB個(gè)人空間;~O)E|*?T&Cy NLS_COMP BINARY S#l.x#ok.f9}24513607ITPUB個(gè)人空間KZ0H~d 15 rows selected.ITPUB個(gè)人空間"r{/o1f mMY specify it in sql statement:ITPUB個(gè)人空間pyn(q"U!i)i}9fmt E Th0V4I%P)B3|(C&R24513607 sql> select to_date('03-SEP-1999','DD-MON-YYYY') from dual; C2u5g;G/XA/u"Z%w(p24513607 03-SEP-99 @|)gU_ly#RR4z24513607 sql> alter session set nls_date_format = 'MM-DD-YYYY';ITPUB個(gè)人空間T{N WBA u Session altered.ITPUB個(gè)人空間b*[%a@[1L"D7c ITPUB個(gè)人空間:dHk^6d rWB sql> select to_date('03-SEP-99','DD-MON-YY') from dual; jY)g8W s Y24513607 TO_DATE('0ITPUB個(gè)人空間"c!c!KCxF&{z ---------- &^DZ h+q[24513607 09-03-1999 )Yu__I^24513607 sql> alter session set nls_date_format = 'RRRR-MM-DD';ITPUB個(gè)人空間 Rh"jX-S Session altered. ITPUB個(gè)人空間i$A G$cF3Q b)W(R ITPUB個(gè)人空間@ \lK$h4LRR sql> select to_date('03-SEP-1999','DD-MON-YYYY') from dual;ITPUB個(gè)人空間{$vAI)pS TO_DATE('0ITPUB個(gè)人空間:j*{&W K jI&g ----------ITPUB個(gè)人空間+k"h3?NW;WbO 1999-09-03 ITPUB個(gè)人空間 Ux j b.j;S Y ITPUB個(gè)人空間5nhFqo$? pk'U When we use TO_CHAR function, we get expected results of format from current sql statement: ITPUB個(gè)人空間j8S~/C bO.^Y sql> alter session set nls_date_format = 'MM-DD-YY';ITPUB個(gè)人空間$W V(LoB%M3ZB Session altered. ^*H&N(wM[d:c-s24513607 sql> select to_char(sysdate,'dd-mm-yyyy') from dual;ITPUB個(gè)人空間V5vD)s)z TO_CHAR(SYSDATE,'DD-MM-YYYY') ITPUB個(gè)人空間F"g.r:m S f4D] 07-09-1999 ITPUB個(gè)人空間,[BP,}#PR sql> alter session set nls_date_format = 'RR-MON-DD'; 2n2|^$y;O2S2? H h24513607 Session altered. *Fr3C.W&pqK7i;g4P24513607 sql> select to_char(sysdate,'dd-mon-yy') from dual;ITPUB個(gè)人空間 O/R&F `8\ Efz TO_CHAR(SYSDATE,'DD-MON-YY') +f\6|e4p3i&`6`,H'T24513607 07-sep-99 ITPUB個(gè)人空間Y s l [#l`;@ sql> select to_char(sysdate,'dd-Mon-yy') from dual; PhA,{0H3smBj0Z24513607 TO_CHAR(SYSDATE,'DD-MON-YY') a.U UG2G:s uC*x24513607 07-Sep-99 3_8le,|5j_c245136078.ITPUB個(gè)人空間x y#hZ!vT+@J_$r select count(*) ITPUB個(gè)人空間0iE;]~!U&Y from ( select rownum-1 rnum ITPUB個(gè)人空間y0T3\%JG from all_objects ITPUB個(gè)人空間jqV"Kb0S4] where rownum <= to_date('2002-02-28','yyyy-mm-dd') - to_date('2002- I5X#s^Kb!Q i-P24513607 02-01','yyyy-mm-dd')+1 Ye;R)N$c)E24513607 ) ITPUB個(gè)人空間o9[0B%g{gf'` where to_char( to_date('2002-02-01','yyyy-mm-dd')+rnum-1, 'D' )ITPUB個(gè)人空間$fJ wg]2a}d not A/o G i&Y*JK0K24513607 in ( '1', '7' ) ,^KU*m*r3Cy v,k24513607 ITPUB個(gè)人空間{VVM+z3`)S 查找2002-02-28至2002-02-01間除星期一和七的天數(shù)ITPUB個(gè)人空間d7C$u,AX@%q4r 在前后分別調(diào)用DBMS_UTILITY.GET_TIME, 讓后將結(jié)果相減(得到的是1/100秒, 而不是毫秒). +f)ew8dU b24513607 ITPUB個(gè)人空間(T_o C$[;V0{H 9.ITPUB個(gè)人空間.] a:Vc!ez SFPd select months_between(to_date('01-31-1999','MM-DD-YYYY'), 0~AB3g+vW2R,J:BZ1k/h24513607 to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL; ITPUB個(gè)人空間 u g+r&t&y}#s6m] 1 R ?FyAy24513607 ITPUB個(gè)人空間DO)n%zaG va select months_between(to_date('02-01-1999','MM-DD-YYYY'), ITPUB個(gè)人空間MS t PFA to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL; ITPUB個(gè)人空間*A1rj w8wzH&C~1x ITPUB個(gè)人空間9g n nH\WL(Q o} 1.03225806451613ITPUB個(gè)人空間P-p _F{Or-} 10. Next_day的用法ITPUB個(gè)人空間!M:g GTSd q Next_day(date, day) ;O wGo7U"}24513607 +CK5n4L_ Bp24513607 Monday-Sunday, for format code DAY ITPUB個(gè)人空間S ZE7d rr Mon-Sun, for format code DY ITPUB個(gè)人空間2cW9oSQ{ L1z.J 1-7, for format code D .\TG2M\0Q24513607 g;g7x'w] ud2451360711 GHVH*V[/f24513607 select to_char(sysdate,'hh:mi:ss') TIME from all_objectsITPUB個(gè)人空間5`-RSLHm8` {;v 注意:第一條記錄的TIME 與最后一行是一樣的ITPUB個(gè)人空間0T0d-XSY"ZuKc 可以建立一個(gè)函數(shù)來(lái)處理這個(gè)問(wèn)題ITPUB個(gè)人空間\vc3pkr2ad2C9x'r2_ create or replace function sys_date return date is lg'`T ~f(KSn1{0M24513607 begin ITPUB個(gè)人空間!t!x;?g K-VFj$q9y RI8` return sysdate; ITPUB個(gè)人空間 jQ?9H N end; ITPUB個(gè)人空間R-T)M8aHn OoM9UP v&lm"DP1P24513607 select to_char(sys_date,'hh:mi:ss') from all_objects; ITPUB個(gè)人空間J6{|:r)Twb 12. $EG8Bj8Md24513607 獲得小時(shí)數(shù)ITPUB個(gè)人空間v8r}4sn0t$Y@ $T!E_]4@W4S2L;s24513607 SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 2:38:40') from offerITPUB個(gè)人空間7o|w.n,Z!v+k sql> select sysdate ,to_char(sysdate,'hh') from dual; &U Q1r5B$mJ:l24513607 ITPUB個(gè)人空間A F?|0g?1J4~ SYSDATE TO_CHAR(SYSDATE,'HH') N_ fr:m2@c9a24513607 -------------------- --------------------- }2|i|N2W"V@24513607 2003-10-13 19:35:21 07 ac(Bj/q2@'c?24513607 ITPUB個(gè)人空間;t$?8VizOH-T/Y sql> select sysdate ,to_char(sysdate,'hh24') from dual;ITPUB個(gè)人空間;v(L0ba4ms0s n3n9^2l W3ed24513607 SYSDATE TO_CHAR(SYSDATE,'HH24') 1N`[(?&F}6YCo b24513607 -------------------- ----------------------- 5X ne0L-M}24513607 2003-10-13 19:35:21 19ITPUB個(gè)人空間0n,Zw8Mv ITPUB個(gè)人空間F7w [.PmOD 獲取年月日與此類似 .i7[ n6I9O2m9c,b K2451360713.ITPUB個(gè)人空間J-`_g#` 年月日的處理ITPUB個(gè)人空間4} PP3vz z3x select older_date, NG3E L"x sl.W24513607 newer_date,ITPUB個(gè)人空間-gS5SbAt"~ years,ITPUB個(gè)人空間vkO3YoH-Hd months,ITPUB個(gè)人空間X LAx aW%s9x abs(ITPUB個(gè)人空間QXRX1@RP(wy trunc( l-M8SXU^2}24513607 newer_date- w:S5t+tE0s24513607 add_months( older_date,years*12+months )ITPUB個(gè)人空間*Y)o.g M4{{ ) &N`/g i!LN8rP bA24513607 ) days $vb%Ju#}*S4O m24513607 from ( select *p6c`2C[24513607 trunc(months_between( newer_date, older_date )/12) YEARS, t)qQhSZk-E24513607 mod(trunc(months_between( newer_date, older_date )), a(pOX!i!sv(n Pt24513607 12 ) MONTHS,ITPUB個(gè)人空間1BK0rl}:HF F newer_date, Lo,W] ~ W^24513607 older_date Xx:M|Y#j*X24513607 from ( select hiredate older_date, ]U L#b&@;l24513607 add_months(hiredate,rownum)+rownum newer_dateITPUB個(gè)人空間z%M g5b6]:] from emp ) /L"DbA.]%a5D$R4N_24513607 )ITPUB個(gè)人空間OC3Cp0I1Wg Zp H t2gK[2QO/O#Bvq2451360714. -M(}#a r)u Q(s1u24513607 處理月份天數(shù)不定的辦法 3N g[J&^ @7n1j.C-D_24513607 select to_char(add_months(last_day(sysdate) +1, -2), 'yyyymmdd'),last_day(sysdate) from dualITPUB個(gè)人空間B\~4l9_8B%xo F ITPUB個(gè)人空間.s1[go#S[fx 16.ITPUB個(gè)人空間[Am"AP(V(H 找出今年的天數(shù) Spw Oo\t24513607 select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dualITPUB個(gè)人空間in\G E"X A 6y*@r(u1x ~24513607 閏年的處理方法 AI:I+?1f2i1D%D24513607 to_char( last_day( to_date('02' || :year,'mmyyyy') ), 'dd' ) {n I4B1d#ZT/F24513607 如果是28就不是閏年 (jE,h&Hq2e1J-e Y24513607 ITPUB個(gè)人空間+z/D4lW.Y*{ 17. Wp&s2[&Y!|i9uB24513607 yyyy與rrrr的區(qū)別ITPUB個(gè)人空間~hT$|u4Y%} 'YYYY99 TO_C (N&g;I$](bV24513607 ------- ---- '|J6f4uT,Hw24513607 yyyy 99 0099 n)MEx.K'L C.c v.q24513607 rrrr 99 1999ITPUB個(gè)人空間:]%X6tp,MK!z yyyy 01 0001 *DJ G a;t&h24513607 rrrr 01 2001 N&d*} W0A:}Fjz24513607 a2l,H)F)|2451360718.不同時(shí)區(qū)的處理ITPUB個(gè)人空間/F"}4hR7V&r,C\)?(F"O select to_char( NEW_TIME( sysdate, 'GMT','EST'), 'dd/mm/yyyy hh:mi:ss') ,sysdate 9tBPQ,y;e7M9`&~H24513607 from dual; 9Q:z_1D0H'Z24513607 ITPUB個(gè)人空間%c%\lH/oU)i-K@9d 19. *vW:P8L$Q IH2x24513607 5秒鐘一個(gè)間隔 \Y"@1J_*I3a!q24513607 Select TO_DATE(FLOOR(TO_CHAR(sysdate,'SSSSS')/300) * 300,'SSSSS') ,TO_CHAR(sysdate,'SSSSS')ITPUB個(gè)人空間s ~V {!YH from dual bqAL/s6V~24513607 ;]E6Cl6g J2eD24513607 2002-11-1 9:55:00 35786ITPUB個(gè)人空間 D&\ r/N)JWL SSSSS表示5位秒數(shù) ~^(V,n-Uo!C24513607 )w;KQ$R&r1R2451360720.ITPUB個(gè)人空間'Nf IOu+z@ A 一年的第幾天 V&]o,m:D"oe^24513607 select TO_CHAR(SYSDATE,'DDD'),sysdate from dual ITPUB個(gè)人空間u9s.a k| 310 2002-11-6 10:03:51ITPUB個(gè)人空間.IYu1S!a ITPUB個(gè)人空間 a/`v9Om 21.計(jì)算小時(shí),分,秒,毫秒ITPUB個(gè)人空間n%E2vf km3^)F)J selectITPUB個(gè)人空間Mw s+N7HZ;B*{VZ Days,ITPUB個(gè)人空間)nsd.}}soB[+w A,ITPUB個(gè)人空間9]N3TQO.B0Of:b TRUNC(A*24) Hours, ?A8FTQZ t&B24513607 TRUNC(A*24*60 - 60*TRUNC(A*24)) Minutes, b+Z X&|QnU3hx24513607 TRUNC(A*24*60*60 - 60*TRUNC(A*24*60)) Seconds,ITPUB個(gè)人空間n#@3l\W!J TRUNC(A*24*60*60*100 - 100*TRUNC(A*24*60*60)) mSecondsITPUB個(gè)人空間 z8Gq UV^Jr7oD from [,j[+mr*b,|)`6XNP24513607 ( A-M$?X^&gK-z3?24513607 select L6T:N e V/? u }Y)@24513607 trunc(sysdate) Days,ITPUB個(gè)人空間 g#PSY$x$J5f{3~N sysdate - trunc(sysdate) AITPUB個(gè)人空間 [YZuuz%g from dualITPUB個(gè)人空間Hv ~/H,oy^sN )ITPUB個(gè)人空間B$U|.l2{4] K ITPUB個(gè)人空間]&Qf;po hU%f%g6@O :bz\f-Lbnu245136079i以上版本 Qlp,YoG24513607 -----------------------------ITPUB個(gè)人空間P;}1t F9uj sql>select to_char(systimestamp,'yyyy-mm-dd hh24:mi:ssxff') time1,ITPUB個(gè)人空間Q+lY a$k to_char(current_timestamp) time2 from dual; @;Zj\.r;X24513607 ITPUB個(gè)人空間X$ol[/QD _O TIME1 TIME2ITPUB個(gè)人空間1\ c,c/e&a-@3a ----------------------------- ---------------------------------------------------------------- n!bnk+W jYb?@h24513607 2003-10-24 10:48:45.656000 24-OCT-03 10.48.45.656000 AM +08:00 NcSYQ6Y&L:In24513607 可以看到,毫秒在to_char中對(duì)應(yīng)的是FF,。 2Z0`G9P\ Pu24513607 ITPUB個(gè)人空間/S_(nZKMt%nv sql> select to_timestamp('2003-10-24 10:48:45.656000','yyyy-mm-dd hh24:mi:ssxff') from dual;ITPUB個(gè)人空間N/XV*{v$N e ITPUB個(gè)人空間E U.u*~$x] TO_TIMESTAMP('2003-10-2410:48:ITPUB個(gè)人空間.[2pd~S;M;V#a -------------------------------------------------ITPUB個(gè)人空間roYt P2__ 24-10月-03 10.48.45.656000000 上午ITPUB個(gè)人空間;\j6smT,gWx*O^ ITPUB個(gè)人空間Fo/g^"V/P~3_ 22. 8b @ OL n[zP p24513607floor((date2-date1) /365) 作為年 g&EF0X iU24513607floor((date2-date1, 365) /30) 作為月 1s,SEwx8y:[|+f|)wB24513607mod(mod(date2-date1, 365), 30)作為日.ITPUB個(gè)人空間.u1f$vvb8zf 23.next_day函數(shù) 2`x)`1r5Ce7a24513607next_day(sysdate,6) 是從當(dāng)前開(kāi)始下一個(gè)星期五,。后面的數(shù)字是從星期日開(kāi)始算起?!?span style="DISPLAY: none">ITPUB個(gè)人空間5hrymA@ 1 2 3 4 5 6 7 ITPUB個(gè)人空間&X4K}'sgsk 日 一 二 三 四 五 六 ITPUB個(gè)人空間L:D"}H0Su1v-Jc.N ITPUB個(gè)人空間m3@.?0t/E;k)x 24.取出一個(gè)時(shí)間段中星期日星期六的天數(shù)ITPUB個(gè)人空間eEKa_8Fn function weekends( p_date1 in date, p_date2 in date ) 'j|9G(?c1[5F_24513607return number \'qO2a ukx0Q24513607as (~'Fk4l Q5F0p)Y&j24513607 l_date1 date default least(p_date1,p_date2); [h@WRd/TQWVX7M24513607 l_date2 date default greatest(p_date1,p_date2);ITPUB個(gè)人空間TkxFO6{p.P5G!Z l_days number default trunc(l_date2-l_date1)+1; %z,? Ey"b24513607 l_cnt number;ITPUB個(gè)人空間 T E9f-Vn*p O` beginITPUB個(gè)人空間_)SHXD%m select count(*) into l_cntITPUB個(gè)人空間3|$V8~S1^8c2^ from (select rownum r s]zg/ID24513607 from all_objects where rownum <= l_days) G+}iS{xfFr:s24513607 where to_char(l_date1+r-1,'dy') in ( 'sat','sun' ); GK4RK pVW24513607 /mq&W'Y*vL'N24513607 return l_cnt;ITPUB個(gè)人空間:p!ggJ |+{\I;Dz7X end;O &?$IIc3x@.u@-]n24513607 hY4R#NB!Q1K24513607ITPUB個(gè)人空間H"NN-`HA X8Cl q,\i&V5Z)n`X24513607ITPUB個(gè)人空間*q(v~zF ITPUB個(gè)人空間7K"Xv:l.v[5U j ,P,H;IH'UB-@2}(r.d-W i24513607ORACLE日期時(shí)間函數(shù)大全 vkZ%]'u` rudY24513607ITPUB個(gè)人空間2H8a3hl:Lw2E6y.h TO_DATE格式(以時(shí)間:2007-11-02 13:45:25為例) (T$C+?!V]F(d2v0_$]24513607 o [f/j#r8y`24513607 Year: doM,Snx24513607 yy two digits 兩位年 顯示值:07 /?'c4Q2u5L*zQKK24513607 yyy three digits 三位年 顯示值:007ITPUB個(gè)人空間`.y1R:i;ZVb!M+J|q1F yyyy four digits 四位年 顯示值:2007ITPUB個(gè)人空間8Iqs dHz!N6|&F1y ] !m5K T abO24513607 Month: PR(xnUc5l e24513607 mm number 兩位月 顯示值:11ITPUB個(gè)人空間)Hgko[!^u mon abbreviated 字符集表示 顯示值:11月,若是英文版,顯示nov ITPUB個(gè)人空間d)Rl&S3` lK month spelled out 字符集表示 顯示值:11月,若是英文版,顯示november f$R,jD/S%Oc24513607 ITPUB個(gè)人空間 l(jOa:J0Pd2t`:a Day: 4g7O8LCr7d24513607 dd number 當(dāng)月第幾天 顯示值:02 %w!}?~/y"t2jt24513607 ddd number 當(dāng)年第幾天 顯示值:02ITPUB個(gè)人空間B$B/jM^5s] dy abbreviated 當(dāng)周第幾天簡(jiǎn)寫(xiě) 顯示值:星期五,若是英文版,顯示fri .t%Ogc/T(dy&`24513607 day spelled out 當(dāng)周第幾天全寫(xiě) 顯示值:星期五,若是英文版,顯示friday 1C*a3M8C"Ne fj24513607 ddspth spelled out, ordinal twelfth R Q'L;p&]-u0Kk24513607 j.uF)Y$w5Z1I*E24513607 Hour:ITPUB個(gè)人空間*~!W#t#u W1B/O hh two digits 12小時(shí)進(jìn)制 顯示值:01 Aw^]#D,_24513607 hh24 two digits 24小時(shí)進(jìn)制 顯示值:13ITPUB個(gè)人空間Zva8D3M8@ ITPUB個(gè)人空間3Ab^`9k(_7~5M Minute: BNX`a]{,X24513607 mi two digits 60進(jìn)制 顯示值:45 r(J$DeR&o&@ E9`/t24513607 ITPUB個(gè)人空間'hWM X%V:h9v.[F Second: B;JT4l%Zlu24513607 ss two digits 60進(jìn)制 顯示值:25ITPUB個(gè)人空間$`3[8|9Q$YHvn"\6z 'ATHe KN|_;m24513607 其它ITPUB個(gè)人空間 PWil JD7` Q digit 季度 顯示值:4ITPUB個(gè)人空間*uEBt xS[/P9{ WW digit 當(dāng)年第幾周 顯示值:44ITPUB個(gè)人空間#n0z3M0zLc.E W digit 當(dāng)月第幾周 顯示值:1 dg8}Y8e*r$c)w24513607 +@*Ooo2oz6z\"p)W24513607 24小時(shí)格式下時(shí)間范圍為: 0:00:00 - 23:59:59.... ITPUB個(gè)人空間z|1V"QB R/U 12小時(shí)格式下時(shí)間范圍為: 1:00:00 - 12:59:59 .... &[2z0H5h5S1R4Hr24513607 ITPUB個(gè)人空間5VW5[|lM 1. 日期和字符轉(zhuǎn)換函數(shù)用法(to_date,to_char)ITPUB個(gè)人空間A+D6a;@s0|(q(O ITPUB個(gè)人空間#Q5|9~UTr m$l select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual; //日期轉(zhuǎn)化為字符串 ITPUB個(gè)人空間3[9y0bLl ymA'mZm^ select to_char(sysdate,'yyyy') as nowYear from dual; //獲取時(shí)間的年 ITPUB個(gè)人空間S%UE8b z j select to_char(sysdate,'mm') as nowMonth from dual; //獲取時(shí)間的月 ITPUB個(gè)人空間y5a"{a(I5Q select to_char(sysdate,'dd') as nowDay from dual; //獲取時(shí)間的日 A8X@$ru5[24513607select to_char(sysdate,'hh24') as nowHour from dual; //獲取時(shí)間的時(shí) N,L1|E6Q.Po2v24513607select to_char(sysdate,'mi') as nowMinute from dual; //獲取時(shí)間的分 yg P;Ad24513607select to_char(sysdate,'ss') as nowSecond from dual; //獲取時(shí)間的秒ITPUB個(gè)人空間/N#Y\bL(C"m ITPUB個(gè)人空間#O5Y2Cx!rtH5T b m X&mx n$ux24513607select to_date('2004-05-07 13:23:44','yyyy-mm-dd hh24:mi:ss') from dual// s-y!ji?`*a24513607ITPUB個(gè)人空間k H%R,plM0\ 2. ITPUB個(gè)人空間Tg1RLe8M select to_char( to_date(222,'J'),'Jsp') from dual ITPUB個(gè)人空間2Dw'qF~7o$~z3v?/J9D ITPUB個(gè)人空間hM%p%qO6[\!z s 顯示Two Hundred Twenty-Two ITPUB個(gè)人空間 a^ja+|V1nA P} ITPUB個(gè)人空間Gx1U;s2]-\;o9[ 3.求某天是星期幾 :lnM jn[Y0F'FWed24513607 select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day') from dual; ITPUB個(gè)人空間OV-d@oG+|9Pc 星期一 l)|4P)s J;TwWfQ]24513607 select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual; ITPUB個(gè)人空間#a-TK4a$e"R*}hk monday | ~ x(fPjC.U24513607 設(shè)置日期語(yǔ)言 ITPUB個(gè)人空間 f$dO~W+y ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN'; ITPUB個(gè)人空間NT0C'Q.FJ1Y 也可以這樣 4JF$}2Y#Y3W X24513607 TO_DATE ('2002-08-26', 'YYYY-mm-dd', 'NLS_DATE_LANGUAGE = American') ITPUB個(gè)人空間?'@!\nYr \'h _#E%O:yo \9r i245136074. 兩個(gè)日期間的天數(shù) ITPUB個(gè)人空間 ?1y-fS(plw%ZAJR[.? select floor(sysdate - to_date('20020405','yyyymmdd')) from dual; ITPUB個(gè)人空間*ob3d/Hy/X]`+Z ITPUB個(gè)人空間6lO?f5A#e5It S 5. 時(shí)間為null的用法 d6?0Js5sU)zK24513607 select id, active_date from table1 ^1fdczv6|wh-MD24513607 UNION Z0M2m2fwrI24513607 select 1, TO_DATE(null) from dual; v2eChF1aD)w24513607 #v RL WX E lqm24513607 注意要用TO_DATE(null) ITPUB個(gè)人空間4v r&th2z ^%| ITPUB個(gè)人空間)@/ceYH 6.月份差 \ Y!Av jp [24513607 a_date between to_date('20011201','yyyymmdd') and to_date('20011231','yyyymmdd') P)UDunwde24513607 那么12月31號(hào)中午12點(diǎn)之后和12月1號(hào)的12點(diǎn)之前是不包含在這個(gè)范圍之內(nèi)的,。 1d k'fFWwFXbNy24513607 所以,當(dāng)時(shí)間需要精確的時(shí)候,,覺(jué)得to_char還是必要的 M/msH1^Ne8j@0s!O24513607 ITPUB個(gè)人空間-])J yC`%A 7. 日期格式?jīng)_突問(wèn)題 ITPUB個(gè)人空間 d9U+~dr:A 輸入的格式要看你安裝的ORACLE字符集的類型, 比如: US7ASCII, date格式的類型就是: '01-Jan-01' 6Jd(bd(]'lx f E24513607 alter system set NLS_DATE_LANGUAGE = American ITPUB個(gè)人空間\ oJIX6nx alter session set NLS_DATE_LANGUAGE = American 0W'}C(Fmg._7O_5R24513607 或者在to_date中寫(xiě) ITPUB個(gè)人空間N2j/U+~'R select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual; (D5Mzl;`3f24513607 注意我這只是舉了NLS_DATE_LANGUAGE,,當(dāng)然還有很多, 0Mv!u)yV g'uKo24513607 可查看 4~L h4m^s$D24513607 select * from nls_session_parameters ITPUB個(gè)人空間T5L(P)_V%Gh @ select * from V$NLS_PARAMETERS Q Q,w7tMd1t3s"W+A24513607ITPUB個(gè)人空間5LX7[{h 8. 3X V&J0X6qR+G24513607 select count(*) ITPUB個(gè)人空間#S8Wt.db)p p from ( select rownum-1 rnum ITPUB個(gè)人空間d Y$X&sd3T3y|6{L%v from all_objects 5E!L4pi!xS24513607 where rownum <= to_date('2002-02-28','yyyy-mm-dd') - to_date('2002- .ObFh Y7A5T!yy?U24513607 02-01','yyyy-mm-dd')+1 ITPUB個(gè)人空間F"iz Z,b lHED ) ITPUB個(gè)人空間;BL3{,d A#Cs} m1^ where to_char( to_date('2002-02-01','yyyy-mm-dd')+rnum-1, 'D' ) Edg9_'R&|24513607 not in ( '1', '7' ) ITPUB個(gè)人空間`r1X kP{.yk h:b?u/~6iAh24513607 查找2002-02-28至2002-02-01間除星期一和七的天數(shù) :]~WB/g ^24513607 在前后分別調(diào)用DBMS_UTILITY.GET_TIME, 讓后將結(jié)果相減(得到的是1/100秒, 而不是毫秒). (W"PHv3}h+v aX24513607ITPUB個(gè)人空間"}/] z gGw 9. 查找月份 /Q5EK]} TH }4MM24513607 select months_between(to_date('01-31-1999','MM-DD-YYYY'),to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL; +FL7b ?tt24513607 1 ITPUB個(gè)人空間5q+DIcNZ select months_between(to_date('02-01-1999','MM-DD-YYYY'),to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL; ITPUB個(gè)人空間{A;lZ)o p~^:L] j 1.03225806451613ITPUB個(gè)人空間QD@ YPN+J ITPUB個(gè)人空間y7S0gw+]7HU6D*Q 10. Next_day的用法 IuT9b k24513607 Next_day(date, day) u@fw5Epr'no ~Z24513607 +Igb9DQyEPTfL$@24513607 Monday-Sunday, for format code DAY 4tn+~d7R5A6t$K24513607 Mon-Sun, for format code DY ITPUB個(gè)人空間g3jVT5H*o 1-7, for format code D ITPUB個(gè)人空間 ukD+|OoF1J m*L;m5x,w\^fKb2451360711 'd;_,LB(W3TJ].i24513607 select to_char(sysdate,'hh:mi:ss') TIME from all_objects ITPUB個(gè)人空間;j _)Jn3a ` L kL 注意:第一條記錄的TIME 與最后一行是一樣的 ITPUB個(gè)人空間,Z(TGk j5M6| 可以建立一個(gè)函數(shù)來(lái)處理這個(gè)問(wèn)題 i*v1v%\3Hvf|%m24513607 create or replace function sys_date return date is +y l"Q;uX:H24513607 begin ITPUB個(gè)人空間n-y*v+Sm^ E return sysdate; #Z#kTjw9L4C$TJJ24513607 end; R1K u1R Y n)I{n24513607 ITPUB個(gè)人空間]om/M,l{ii!l select to_char(sys_date,'hh:mi:ss') from all_objects; ITPUB個(gè)人空間:u4O8|$h ~]+B ^ !Ot@`4m*?2451360712.獲得小時(shí)數(shù) 1Y6OW5SXN0^9d24513607 extract()找出日期或間隔值的字段值ITPUB個(gè)人空間Is4Ds9q%j SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 2:38:40') from offer ITPUB個(gè)人空間 D+z(L[kAu\7FT SQL> select sysdate ,to_char(sysdate,'hh') from dual; ITPUB個(gè)人空間1S%S1g1a?6A1B!z7r7p(i g ITPUB個(gè)人空間}Ls)Fb(e@L SYSDATE TO_CHAR(SYSDATE,'HH') T9N,p].~ij5}[24513607 -------------------- --------------------- ITPUB個(gè)人空間b&u+i;?z |8EK%e 2003-10-13 19:35:21 07 ITPUB個(gè)人空間B9]$n|+[Y~ UP -ZL6p-cjs(yQI24513607 SQL> select sysdate ,to_char(sysdate,'hh24') from dual; ITPUB個(gè)人空間YX@g){ Wa 7U7Q!^{#F.|;C24513607 SYSDATE TO_CHAR(SYSDATE,'HH24') G \5|6F4K)wIca24513607 -------------------- ----------------------- ITPUB個(gè)人空間K-cn^%Q@.q3S` 2003-10-13 19:35:21 19 ITPUB個(gè)人空間"vkbr+\$Kj1[V ITPUB個(gè)人空間uH#]Cs$?Q fq"] ITPUB個(gè)人空間8W m4b(] nb Q N 13.年月日的處理 ITPUB個(gè)人空間Ze1~9l;Zq select older_date, ITPUB個(gè)人空間Ap"{w(l7r newer_date, ITPUB個(gè)人空間!~qiYJ"YHdyR years, ITPUB個(gè)人空間8[ l8a8O%u,n months, ITPUB個(gè)人空間dD|5g:@-ApC abs( ITPUB個(gè)人空間fRCFod'c trunc( ITPUB個(gè)人空間 f}2^.MeP^G newer_date- ITPUB個(gè)人空間!^%Z\C4n0co)f add_months( older_date,years*12+months ) f8\&Ezn24513607 ) 4N,mPS'|t"kK6Cs24513607 ) daysITPUB個(gè)人空間k#@ V){ P.k ITPUB個(gè)人空間 ]'Ov'p)y \l from ( select ITPUB個(gè)人空間(h M[1]o trunc(months_between( newer_date, older_date )/12) YEARS, ITPUB個(gè)人空間 G)Y!J{C.X mod(trunc(months_between( newer_date, older_date )),12 ) MONTHS, ITPUB個(gè)人空間7n*Lr M2F _S)o newer_date, ITPUB個(gè)人空間C1_6rt }I older_date 1J jj/|Ps&r24513607 from ( pY$_AA1|l l#\'H,m24513607 select hiredate older_date, add_months(hiredate,rownum)+rownum newer_date ITPUB個(gè)人空間+R}Ckn2?+G5H from emp 8o:d jT+Yn.H k~@7m24513607 ) ITPUB個(gè)人空間1j|F.f'a ) "Q,L.LH3a\w8H24513607ITPUB個(gè)人空間yIP(B,T2cg 14.處理月份天數(shù)不定的辦法 ITPUB個(gè)人空間&V"?z/m.W5@t4NP select to_char(add_months(last_day(sysdate) +1, -2), 'yyyymmdd'),last_day(sysdate) from dual *Y1g.r-I:U24513607ITPUB個(gè)人空間"nbv6z(g9w l ve2O 16.找出今年的天數(shù) OX(Jl;F3w24513607 select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dual &hB]G,N;aA)d"Ol24513607 %l s?|_M2Y24513607 閏年的處理方法 ITPUB個(gè)人空間7}2ZPr+|wy&P to_char( last_day( to_date('02' | | :year,'mmyyyy') ), 'dd' ) ITPUB個(gè)人空間I^6q!N9V*{ 如果是28就不是閏年 ITPUB個(gè)人空間e4nia$n#u.^0q'CGq0e PP;O&waz@2451360717.yyyy與rrrr的區(qū)別 %CybN+DA24513607 'YYYY99 TO_C ITPUB個(gè)人空間 KPh u,Tm6f,PW ------- ---- MQP7BJ24513607 yyyy 99 0099 or Q1W6@H24513607 rrrr 99 1999 aJ` |m2H9} t24513607 yyyy 01 0001 0?R1\;Lq3Hr|1h24513607 rrrr 01 2001 ITPUB個(gè)人空間3a[2\Hr,Ap (s)lS3D)cN?,g2451360718.不同時(shí)區(qū)的處理 ITPUB個(gè)人空間gH(g,|m select to_char( NEW_TIME( sysdate, 'GMT','EST'), 'dd/mm/yyyy hh:mi:ss') ,sysdate ITPUB個(gè)人空間9QL9{ u4`Rqr@ ^v[&W from dual; R,u$E1dK24513607ITPUB個(gè)人空間(gks%^H^H-_f:g 19.5秒鐘一個(gè)間隔 J6^H f D6zl!l24513607 Select TO_DATE(FLOOR(TO_CHAR(sysdate,'SSSSS')/300) * 300,'SSSSS') ,TO_CHAR(sysdate,'SSSSS') "D4d%Y g1X.m24513607 from dual ITPUB個(gè)人空間5d ?9_-MuE TE9c}:g_4P(yU,T:E-H24513607 2002-11-1 9:55:00 35786 ITPUB個(gè)人空間9Z*[D\!P-M0o/F,V#@ SSSSS表示5位秒數(shù) ITPUB個(gè)人空間 h6{f2Dl| d CxU'I1| Q2451360720.一年的第幾天 7Q$J6I{jr k24513607 select TO_CHAR(SYSDATE,'DDD'),sysdate from dualITPUB個(gè)人空間.uqO/A#Z ITPUB個(gè)人空間T_"X4\Ze+Ds a^ 310 2002-11-6 10:03:51 wm:[E4A24513607 J5FZ ] bK[y6B2451360721.計(jì)算小時(shí),分,秒,毫秒 ITPUB個(gè)人空間1sV/CEygz*o)h select +tj;q g{wr*\H:[24513607 Days, ITPUB個(gè)人空間,E2f?x"](Z4_*Y A, ITPUB個(gè)人空間1s8l/S~@u TRUNC(A*24) Hours, ke6RI/B U24513607 TRUNC(A*24*60 - 60*TRUNC(A*24)) Minutes, j?&h?)A`/gA"B24513607 TRUNC(A*24*60*60 - 60*TRUNC(A*24*60)) Seconds, ITPUB個(gè)人空間n3qu"LN _6X j}n TRUNC(A*24*60*60*100 - 100*TRUNC(A*24*60*60)) mSeconds ITPUB個(gè)人空間^8XRk@,v-^P from ITPUB個(gè)人空間6s~8Tbdx%|ks ( 7q2Pki KsId24513607 select ITPUB個(gè)人空間O7K{8Y B!r [ trunc(sysdate) Days, ITPUB個(gè)人空間(@x!f;ebV9qt:W sysdate - trunc(sysdate) A %V9`+g {#O f c}Vz24513607 from dual ITPUB個(gè)人空間%nb4z Y&i.rTFt/? ) ITPUB個(gè)人空間|H)a yt{"`-j!uX}.J :K'SvZ p4h24513607ITPUB個(gè)人空間n bsZxu select * from tabname &T I4zg oo%e24513607 order by decode(mode,'FIFO',1,-1)*to_char(rq,'yyyymmddhh24miss'); ITPUB個(gè)人空間d @ }H$K$wD ITPUB個(gè)人空間*VRMM ]2T // ]0Z@5k H4v*?%v24513607 floor((date2-date1) /365) 作為年 O l'S)F(m \)CAb24513607 floor((date2-date1, 365) /30) 作為月 ITPUB個(gè)人空間bjW1euk+iEAr d(mod(date2-date1, 365), 30)作為日. HGXH0O^ u24513607ITPUB個(gè)人空間y#A-fVb~3g(Y 23.next_day函數(shù) 返回下個(gè)星期的日期,day為1-7或星期日-星期六,1表示星期日ITPUB個(gè)人空間(}iA%c'F,B5D!X next_day(sysdate,6)是從當(dāng)前開(kāi)始下一個(gè)星期五,。后面的數(shù)字是從星期日開(kāi)始算起,。 t GE!D3j _ {3jfq24513607 1 2 3 4 5 6 7 ITPUB個(gè)人空間6l;f/i3M.gsXL2[ 日 一 二 三 四 五 六 #Ku Jic2m24513607 &@M'|%~3S24513607 --------------------------------------------------------------- 8Gx ZsR,Q3oy24513607 ITPUB個(gè)人空間%`I#d(^W&} select (sysdate-to_date('2003-12-03 12:55:45','yyyy-mm-dd hh24:mi:ss'))*24*60*60 from ddual mzd4m"fs24513607 日期 返回的是天 然后 轉(zhuǎn)換為ssITPUB個(gè)人空間 G*s`9_\P ITPUB個(gè)人空間~dz6j7PC(K%Hp9U$d 24,round[舍入到最接近的日期](day:舍入到最接近的星期日) ;hzE,hG[rXSm2N(H24513607 select sysdate S1, &~n6vdb`/Z5O24513607 round(sysdate) S2 , (R!?m,n3S.z8iq;I)y24513607 round(sysdate,'year') YEAR,ITPUB個(gè)人空間c^/~9l*` round(sysdate,'month') MONTH ,ITPUB個(gè)人空間hh;K#hEsN| round(sysdate,'day') DAY from dual Y|#d X?x/h24513607 oV3[8w Q@R2451360725,trunc[截?cái)嗟阶罱咏娜掌?單位為天] ,返回的是日期類型 (}.uufPcH,lS4Zu24513607 select sysdate S1, XV/Z;r A5Q:T6t9[24513607 trunc(sysdate) S2, //返回當(dāng)前日期,無(wú)時(shí)分秒ITPUB個(gè)人空間0\kx:].Dr.c trunc(sysdate,'year') YEAR, //返回當(dāng)前年的1月1日,無(wú)時(shí)分秒ITPUB個(gè)人空間-P+QOfLW,l}p trunc(sysdate,'month') MONTH , //返回當(dāng)前月的1日,無(wú)時(shí)分秒 ,X1T!@IlJ$x24513607 trunc(sysdate,'day') DAY //返回當(dāng)前星期的星期天,無(wú)時(shí)分秒ITPUB個(gè)人空間%l9|*co"H,fZ$C4_9[$f from dual *F |d o)Fwe-x24513607ITPUB個(gè)人空間5As:@~ ~kLIC 26,返回日期列表中最晚日期 @+Fk:Q\2JJv24513607 select greatest('01-1月-04','04-1月-04','10-2月-04') from dual "F(R8l u+~-S+x24513607 'C7D;EZ"N!V||)e2451360727.計(jì)算時(shí)間差 .f5{#d \$y24513607 注:oracle時(shí)間差是以天數(shù)為單位,所以換算成年月,日 rf f"Ib m'P w$h?!WE24513607 ITPUB個(gè)人空間e"Rm/B/UU _ select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))/365) as spanYears from dual //時(shí)間差-年 7Aj5IrY2DPE24513607 select ceil(moths_between(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))) as spanMonths from dual //時(shí)間差-月 a ew.fZuV@24513607 select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))) as spanDays from dual //時(shí)間差-天ITPUB個(gè)人空間)Ww)G+R|(` select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24) as spanHours from dual //時(shí)間差-時(shí)ITPUB個(gè)人空間ty^j2TKS 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 //時(shí)間差-分ITPUB個(gè)人空間#u+I UYT]!m,L 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 //時(shí)間差-秒ITPUB個(gè)人空間Y~4z|+^(J1JI #c/YD3H4UgM2451360728.更新時(shí)間ITPUB個(gè)人空間'e#F-G'I@ 注:oracle時(shí)間加減是以天數(shù)為單位,設(shè)改變量為n,所以換算成年月,日ITPUB個(gè)人空間e$`V o:M.K 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 //改變時(shí)間-年 (k5C-bd R3|$i,S3f24513607 select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),add_months(sysdate,n) as newTime from dual //改變時(shí)間-月ITPUB個(gè)人空間#NlkY;{ select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),to_char(sysdate+n,'yyyy-mm-dd hh24:mi:ss') as newTime from dual //改變時(shí)間-日ITPUB個(gè)人空間 nf\'BXsC 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 //改變時(shí)間-時(shí) Ke N^;_p5k/cRR24513607 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 //改變時(shí)間-分 L)?;yV5n qUUE2m!R24513607 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 //改變時(shí)間-秒ITPUB個(gè)人空間,h"LK8Q'mtTz(Sy2R5k ITPUB個(gè)人空間MjaX zxj 29.查找月的第一天,最后一天ITPUB個(gè)人空間6J.s'@X*QlF SELECT Trunc(Trunc(SYSDATE, 'MONTH') - 1, 'MONTH') First_Day_Last_Month, (J+Ww#ZQ7q"oC24513607 Trunc(SYSDATE, 'MONTH') - 1 / 86400 Last_Day_Last_Month, 5L0A.W Mf A6hL ys3p24513607 Trunc(SYSDATE, 'MONTH') First_Day_Cur_Month,ITPUB個(gè)人空間2D*Ou%pW;b:Ta5_ LAST_DAY(Trunc(SYSDATE, 'MONTH')) + 1 - 1 / 86400 Last_Day_Cur_Month ![.mhg z:B%gm~[ m24513607 FROM dual; a a(L!Uj6y24513607 .R#] u4l\&]3w,s!v.po|24513607ITPUB個(gè)人空間 sz`,Fl 三. 字符函數(shù)(可用于字面字符或數(shù)據(jù)庫(kù)列) %XLK\BwJ24513607 r Z!D'Uh9xTI*Q245136071,字符串截取 (b-\,M q+_x9F~2V24513607 select substr('abcdef',1,3) from dualITPUB個(gè)人空間bx m I4L ITPUB個(gè)人空間,v q-\4^o Q|&` 2,查找子串位置 q+z/gP/Vf"_e24513607 select instr('abcfdgfdhd','fd') from dualITPUB個(gè)人空間_[ gc+@z1vV D$W(w`L245136073,字符串連接ITPUB個(gè)人空間*g*u]$l;c]9DX select 'HELLO'||'hello world' from dual;ITPUB個(gè)人空間R"y$jS R#O6sRI ITPUB個(gè)人空間3kb DF K 4, 1)去掉字符串中的空格 z |fL}24513607 select ltrim(' abc') s1, R:}9gRO2y\24513607 rtrim('zhang ') s2,ITPUB個(gè)人空間!wPRC#K"?3l$y:M trim(' zhang ') s3 from dualITPUB個(gè)人空間k%A0X4E1}4pZ7a 2)去掉前導(dǎo)和后綴ITPUB個(gè)人空間Y3xx RC| fO select trim(leading 9 from 9998767999) s1,ITPUB個(gè)人空間jI|/_RU8P5@ trim(trailing 9 from 9998767999) s2, 7E-R P"I:x/`"Y3K24513607 trim(9 from 9998767999) s3 from dual; w^d'{kM9P*s#l24513607 8}xW'Eq,z1jG ]+r245136075, 返回字符串首字母的Ascii值 r}e1T)u3A ]b s24513607 select ascii('a') from dualITPUB個(gè)人空間p \"J/oFEv ITPUB個(gè)人空間(yKQ uar 6,返回ascii值對(duì)應(yīng)的字母 -El @a?Wi:p {;C_24513607 select chr(97) from dual cVXw$bO'ZE@j}24513607 /O9u{9d,F)v(o!@ a j245136077,計(jì)算字符串長(zhǎng)度 t.n5wj,H lN24513607 select length('abcdef') from dual D[`+nc#^hF9Noo24513607 :I QU M&k/qg]%Q*i1V/K245136078,initcap(首字母變大寫(xiě)) ,lower(變小寫(xiě)),upper(變大寫(xiě))ITPUB個(gè)人空間 f*f;};L$NBaz$C'd select lower('ABC') s1, 5zL6[w5J)d#?6aE}24513607 upper('def') s2, 4vTJ?_'I$A24513607 initcap('efg') s3ITPUB個(gè)人空間5d*|Y#g|)M Yn~1\ from dual;ITPUB個(gè)人空間Jrrp%?'pp ITPUB個(gè)人空間^{p DO[4@ 9,ReplaceITPUB個(gè)人空間/r!LoVlNu!c#| select replace('abc','b','xy') from dual; 7KA-`,Th$?m24513607 /\*Ie3SL-A;^/\I fW2451360710,translate $],h9J4x:r |%f0O L24513607 select translate('abc','b','xx') from dual; -- x是1位ITPUB個(gè)人空間$Gt;mc.k!Qu/X ITPUB個(gè)人空間LB b QF@ 11,lpad [左添充] rpad [右填充](用于控制輸出格式)ITPUB個(gè)人空間L$qU \tI)z!t'Cq select lpad('func',15,'=') s1, rpad('func',15,'-') s2 from dual;ITPUB個(gè)人空間9b6} {o'\uS"?g1|/m select lpad(dname,14,'=') from dept;ITPUB個(gè)人空間9O7fpcdD 2jz(@#J{N2451360712, decode[實(shí)現(xiàn)if ..then 邏輯] 注:第一個(gè)是表達(dá)式,最后一個(gè)是不滿足任何一個(gè)條件的值 -{mE_!U#X`i24513607 select deptno,decode(deptno,10,'1',20,'2',30,'3','其他') from dept;ITPUB個(gè)人空間,UTx6T8t6@wSk%cT 例: P2y5MCK j24513607 select seed,account_name,decode(seed,111,1000,200,2000,0) from t_userInfo//如果seed為111,則取1000;為200,取2000;其它取0ITPUB個(gè)人空間c n4QF ].}+PW'd{2a select seed,account_name,decode(sign(seed-111),1,'big seed',-1,'little seed','equal seed') from t_userInfo//如果seed>111,則顯示大;為200,則顯示小;其它則顯 f'^L&C(RD24513607ITPUB個(gè)人空間'Rp1VR5AH 示相等ITPUB個(gè)人空間Q V,e"A:H7`-Ah5w$U ?,JY-TN2K4s\2Uo\2451360713 case[實(shí)現(xiàn)switch ..case 邏輯] Zjq"|H:W24513607 SELECT CASE X-FIELDITPUB個(gè)人空間 v'u%u ~K1~x WHEN X-FIELD < 40 THEN 'X-FIELD 小于 40'ITPUB個(gè)人空間yx rP|&]%OH"D)e WHEN X-FIELD < 50 THEN 'X-FIELD 小于 50' 6Mis ma2~qx24513607 WHEN X-FIELD < 60 THEN 'X-FIELD 小于 60'ITPUB個(gè)人空間^$g4ONfZt d&a:`&AJ ELSE 'UNBEKNOWN' K ~.Nx%Sh24513607 END ^@i,U%v5@k(V24513607 FROM DUALITPUB個(gè)人空間5j;?N#aTF ITPUB個(gè)人空間'T/EW!{&t(swH'zX 注:CASE語(yǔ)句在處理類似問(wèn)題就顯得非常靈活,。當(dāng)只是需要匹配少量數(shù)值時(shí),,用Decode更為簡(jiǎn)潔。ITPUB個(gè)人空間r$h:X"V.nswp A J Y3T2oXm)`24513607四.數(shù)字函數(shù) 1Qd8Fz4?!@ gU4I245136071,,取整函數(shù)(ceil 向上取整,floor 向下取整) Af jFr+B.r$[#bi24513607 select ceil(66.6) N1,floor(66.6) N2 from dual; c ^ RZcZ%b+wP&^24513607 k;n0Rm(wQ:Y245136072, 取冪(power) 和 求平方根(sqrt)ITPUB個(gè)人空間;Cs s x}wr K:T+q^J select power(3,2) N1,sqrt(9) N2 from dual; Dx/mP4R24513607 )Q\1J8r r c g2qk U245136073,求余ITPUB個(gè)人空間5EIg+Oq0X-s select mod(9,5) from dual;ITPUB個(gè)人空間uRt1b ?3o;xe1A*H ITPUB個(gè)人空間 t2p6W0M{ 4,返回固定小數(shù)位數(shù) (round:四舍五入,,trunc:直接截?cái)? 0pb y't9]'q24513607 select round(66.667,2) N1,trunc(66.667,2) N2 from dual;ITPUB個(gè)人空間~S)~V^E-Y'v9O ITPUB個(gè)人空間N%C;}{\uX^Oi 5,返回值的符號(hào)(正數(shù)返回為1,負(fù)數(shù)為-1) J$t#l4jh,m$U24513607 select sign(-32),sign(293) from dual;ITPUB個(gè)人空間.nE'sB2_2O*ur e1xuNw*T}'w24513607五.轉(zhuǎn)換函數(shù)ITPUB個(gè)人空間!t:TD"olsj]x 1,,to_char()[將日期和數(shù)字類型轉(zhuǎn)換成字符類型] Uv2@#Ye+T24513607 1) select to_char(sysdate) s1, 0S e| S@h9S_24513607 to_char(sysdate,'yyyy-mm-dd') s2, yo+TZmOi'B?24513607 to_char(sysdate,'yyyy') s3, i?o~$u@QI24513607 to_char(sysdate,'yyyy-mm-dd hh12:mi:ss') s4,ITPUB個(gè)人空間1LX*dqz3h'y dA to_char(sysdate, 'hh24:mi:ss') s5,ITPUB個(gè)人空間{W*e\` LEr to_char(sysdate,'DAY') s6 a+d@#S,T2N`24513607 from dual;ITPUB個(gè)人空間ZIk6ia 2) select sal,to_char(sal,'$99999') n1,to_char(sal,'$99,999') n2 from empITPUB個(gè)人空間+@0KKo#\z @6p(Y!bi FE_3\ m245136072, to_date()[將字符類型轉(zhuǎn)換為日期類型] p]l/NorK&[24513607 insert into emp(empno,hiredate) values(8000,to_date('2004-10-10','yyyy-mm-dd'));ITPUB個(gè)人空間E.A{d8z+g ITPUB個(gè)人空間4\Gy-Q XL?E t 3, to_number() 轉(zhuǎn)換為數(shù)字類型ITPUB個(gè)人空間C\7vz ue]5^ select to_number(to_char(sysdate,'hh12')) from dual; //以數(shù)字顯示的小時(shí)數(shù) f5O AXm24513607 ITPUB個(gè)人空間*NN3XTM3I2oK%J 六.其他函數(shù)ITPUB個(gè)人空間k#M r+B!D:C$v{ 1.user: `-eV ?4L$_x24513607 返回登錄的用戶名稱 %vU,k7l|,H,y6j24513607 select user from dual;ITPUB個(gè)人空間0NI'Bt*}[ g\Gw3}o,gW m24513607 2.vsize: +Y6?5l}8o }*Fj;S24513607 返回表達(dá)式所需的字節(jié)數(shù) p },q%g5csZR24513607 select vsize('HELLO') from dual; p2_@X-x*z0o%w24513607 ITPUB個(gè)人空間K;?8MOO@-q@G 3.nvl(ex1,ex2): gs|LtW24513607 ex1值為空則返回ex2,否則返回該值本身ex1(常用)ITPUB個(gè)人空間 }b-_t'g,O`3{Si 例:如果雇員沒(méi)有傭金,將顯示0,,否則顯示傭金 E&X/wF O8s24513607 select comm,nvl(comm,0) from emp;ITPUB個(gè)人空間_rP|;v)h%Ve F)J#LD-jk$`E9X24513607 4.nullif(ex1,ex2):ITPUB個(gè)人空間;er5vT;YS%SwK 值相等返空,,否則返回第一個(gè)值 r9C(^7` u@24513607 例:如果工資和傭金相等,則顯示空,,否則顯示工資ITPUB個(gè)人空間}H7|'h2B)w]m select nullif(sal,comm),sal,comm from emp; b/T"\ b/s_&g]yG24513607 ITPUB個(gè)人空間:j'y:eR!jC-hq 5.coalesce: :M4pm2w^ S g&ihR24513607 返回列表中第一個(gè)非空表達(dá)式 j(~ R)y"Z&G$Stq24513607 select comm,sal,coalesce(comm,sal,sal*10) from emp;ITPUB個(gè)人空間u s GT HX)E4zi ~ TWvJ'Q24513607 6.nvl2(ex1,ex2,ex3) : j;o1[ zh)i:z+rk24513607 如果ex1不為空,,顯示ex2,否則顯示ex3ITPUB個(gè)人空間&td$f(Yvex(Q 如:查看有傭金的雇員姓名以及他們的傭金 ,r Jc[W24513607 select nvl2(comm,ename,') as HaveCommName,comm from emp;ITPUB個(gè)人空間6I|#t2wE$\%S ITPUB個(gè)人空間dwkY.o ITPUB個(gè)人空間nDOK,s.kbPW%`XV 七.分組函數(shù)ITPUB個(gè)人空間_Ij*@(T max min avg count sumITPUB個(gè)人空間Vx,Fz;Y/mP UW;p 1,整個(gè)結(jié)果集是一個(gè)組ITPUB個(gè)人空間6yFu;B ka? 1) 求部門(mén)30 的最高工資,,最低工資,平均工資,,總?cè)藬?shù),,有工作的人數(shù),,工種數(shù)量及工資總和ITPUB個(gè)人空間 m8c/rsN!~ @H0B F O select max(ename),max(sal),ITPUB個(gè)人空間)? t+M[L9z,T min(ename),min(sal),ITPUB個(gè)人空間:`i;^F1^+x%k%k_ avg(sal),ITPUB個(gè)人空間 Im?eMiE count(*) ,count(job),count(distinct(job)) , 8S6OK^;K!f24513607 sum(sal) from emp where deptno=30; 4n7L+j Z-T]R/Z u R245136072, 帶group by 和 having 的分組ITPUB個(gè)人空間FI.VcRD mX 1)按部門(mén)分組求最高工資,最低工資,,總?cè)藬?shù),,有工作的人數(shù),工種數(shù)量及工資總和ITPUB個(gè)人空間_i4?6j4De X jW select deptno, max(ename),max(sal), 6Y"oW"})A6i?*tK rQ24513607 min(ename),min(sal),ITPUB個(gè)人空間2G:\K8ze.C avg(sal),ITPUB個(gè)人空間x3]-\;[xt&_7W V N'c count(*) ,count(job),count(distinct(job)) , d^ hM-|.R(lz24513607 sum(sal) from emp group by deptno; )W|{Z4XW8h24513607 R&O5y1|O2Hp P24513607 2)部門(mén)30的最高工資,,最低工資,,總?cè)藬?shù),有工作的人數(shù),,工種數(shù)量及工資總和ITPUB個(gè)人空間8QPf/ke select deptno, max(ename),max(sal),ITPUB個(gè)人空間 Td.pE'h/N,WZv min(ename),min(sal), [7Y5Msp[24513607 avg(sal),ITPUB個(gè)人空間m$T[Gc k count(*) ,count(job),count(distinct(job)) ,ITPUB個(gè)人空間/A:N5P2UG1|tf5c sum(sal) from emp group by deptno having deptno=30;ITPUB個(gè)人空間Z@2o3g*R\3v l5w ITPUB個(gè)人空間E:j6x"x.v:j9m 3, stddev 返回一組值的標(biāo)準(zhǔn)偏差ITPUB個(gè)人空間W\C.x m+ov select deptno,stddev(sal) from emp group by deptno; \wb5f5m!@)}*y"b+i24513607 variance 返回一組值的方差差ITPUB個(gè)人空間7E#RgW} select deptno,variance(sal) from emp group by deptno;ITPUB個(gè)人空間.?!y/kl-K8L%w Tq po*{;X'@$u T245136074, 帶有rollup和cube操作符的Group ByITPUB個(gè)人空間%fO%k'\V [omU-e rollup 按分組的第一個(gè)列進(jìn)行統(tǒng)計(jì)和最后的小計(jì)ITPUB個(gè)人空間2BU[RL cube 按分組的所有列的進(jìn)行統(tǒng)計(jì)和最后的小計(jì)ITPUB個(gè)人空間,BC D`*Cq8n select deptno,job ,sum(sal) from emp group by deptno,job;ITPUB個(gè)人空間@7dL;V { g select deptno,job ,sum(sal) from emp group by rollup(deptno,job);ITPUB個(gè)人空間+W k+T6XG(V a;^2N cube 產(chǎn)生組內(nèi)所有列的統(tǒng)計(jì)和最后的小計(jì)ITPUB個(gè)人空間%o3h1d1R@:T }0P select deptno,job ,sum(sal) from emp group by cube(deptno,job); *\3e&?#`;zcD24513607ITPUB個(gè)人空間1uV RT#d/v)M 八,、臨時(shí)表ITPUB個(gè)人空間 ^[x Or 只在會(huì)話期間或在事務(wù)處理期間存在的表.ITPUB個(gè)人空間(uOUb8cR&XL 臨時(shí)表在插入數(shù)據(jù)時(shí),動(dòng)態(tài)分配空間 aG2d%~0\9Uj24513607 create global temporary table temp_dept '~2N1L#y7l_g24513607 (dno number,ITPUB個(gè)人空間9O&Y#]1V0|m'a dname varchar2(10)) wiw/O5Y^ Wi24513607 on commit delete rows; U.O;z3a-pV#H\e24513607 insert into temp_dept values(10,'ABC'); R2K TX Lu+Sw"]6p24513607 commit; o i4zc R&H[o~ _0~24513607 select * from temp_dept; --無(wú)數(shù)據(jù)顯示,數(shù)據(jù)自動(dòng)清除ITPUB個(gè)人空間f9jp2VOuG g on commit preserve rows:在會(huì)話期間表一直可以存在(保留數(shù)據(jù))ITPUB個(gè)人空間3^H5Zk-~)\&j)@ on commit delete rows:事務(wù)結(jié)束清除數(shù)據(jù)(在事務(wù)結(jié)束時(shí)自動(dòng)刪除表的數(shù)據(jù)) w&Z(mq9Y24513607 |
|