【正文】
mdd hh24:mi:ss39。) as newTime from dual //改變時間年select to_char(sysdate,39。yyyymmdd hh24:mi:ss39。),add_months(sysdate,n) as newTime from dual //改變時間月select to_char(sysdate,39。yyyymmdd hh24:mi:ss39。),to_char(sysdate+n,39。yyyymmdd hh24:mi:ss39。) as newTime from dual //改變時間日select to_char(sysdate,39。yyyymmdd hh24:mi:ss39。),to_char(sysdate+n/24,39。yyyymmdd hh24:mi:ss39。) as newTime from dual //改變時間時select to_char(sysdate,39。yyyymmdd hh24:mi:ss39。),to_char(sysdate+n/24/60,39。yyyymmdd hh24:mi:ss39。) as newTime from dual //改變時間分select to_char(sysdate,39。yyyymmdd hh24:mi:ss39。),to_char(sysdate+n/24/60/60,39。yyyymmdd hh24:mi:ss39。) as newTime from dual //改變時間秒,最后一天SELECT Trunc(Trunc(SYSDATE, 39。MONTH39。) 1, 39。MONTH39。) First_Day_Last_Month,Trunc(SYSDATE, 39。MONTH39。) 1 / 86400 Last_Day_Last_Month,Trunc(SYSDATE, 39。MONTH39。) First_Day_Cur_Month,LAST_DAY(Trunc(SYSDATE, 39。MONTH39。)) + 1 1 / 86400 Last_Day_Cur_MonthFROM dual。轉(zhuǎn)換函數(shù) 與date操作關(guān)系最大的就是兩個轉(zhuǎn)換函數(shù):to_date(),to_char() to_date() 作用將字符類型按一定格式轉(zhuǎn)化為日期類型: 具體用法:to_date(39。39。2004112739。39。,39。39。yyyymmdd39。39。),前者為字符串,后者為轉(zhuǎn)換日期格式,注意,前后兩者要以一對應(yīng)。 如。to_date(39。39。20041127 13:34:4339。39。, 39。39。yyyymmdd hh24:mi:ss39。39。) 將得到具體的時間 多種日期格式: YYYY:四位表示的年份 YYY,YY,Y:年份的最后三位、兩位或一位,缺省為當(dāng)前世紀(jì) MM:01~12的月份編號 MONTH:九個字符表示的月份,右邊用空格填補(bǔ) MON:三位字符的月份縮寫 WW:一年中的星期 D:星期中的第幾天 DD:月份中的第幾天 DDD:年所中的第幾天 DAY:九個字符表示的天的全稱,右邊用空格補(bǔ)齊 HH,HH12:一天中的第幾個小時,12進(jìn)制表示法 HH24:一天中的第幾個小時,取值為00~23 MI:一小時中的分鐘 SS:一分鐘中的秒 SSSS:從午夜開始過去的秒數(shù) to_char():將日期轉(zhuǎn)按一定格式換成字符類型 SQL select to_char(sysdate,39。39。yyyymmdd hh24:mi:ss39。39。) time from dual。 TIME 20041008 15:22:58 即把當(dāng)前時間按yyyymmdd hh24:mi:ss格式轉(zhuǎn)換成字符類型 在oracle中處理日期大全 TO_DATE格式 Day: dd number 12 dy abbreviated fri day spelled out friday ddspth spelled out, ordinal twelfth Month: mm number 03 mon abbreviated mar month spelled out march Year: yy two digits 98 yyyy four digits 1998 24小時格式下時間范圍為: 0:00:00 23:59:59.... 12小時格式下時間范圍為: 1:00:00 12:59:59 .... [ZT]日期和??函??? 1. 日期和字符轉(zhuǎn)換函數(shù)用法(to_date,to_char) 2. select to_char( to_date(222,39。39。J39。39。),39。39。Jsp39。39。) from dual 顯示Two Hundred TwentyTwo 3. 求某天是星期幾 select to_char(to_date(39。39。2002082639。39。,39。39。yyyymmdd39。39。),39。39。day39。39。) from dual。 星期一 select to_char(to_date(39。39。2002082639。39。,39。39。yyyymmdd39。39。),39。39。day39。39。,39。39。NLS_DATE_LANGUAGE = American39。39。) from dual。 monday 設(shè)置日期語言 ALTER SESSION SET NLS_DATE_LANGUAGE=39。39。AMERICAN39。39。 也可以這樣 TO_DATE (39。39。2002082639。39。, 39。39。YYYYmmdd39。39。, 39。39。NLS_DATE_LANGUAGE = American39。39。) 4. 兩個日期間的天數(shù) select floor(sysdate to_date(39。39。2002040539。39。,39。39。yyyymmdd39。39。)) 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(39。39。2001120139。39。,39。39。yyyymmdd39。39。) and to_date(39。39。2001123139。39。,39。39。yyyymmdd39。39。) 那么12月31號中午12點之后和12月1號的12點之前是不包含在這個范圍之內(nèi)的。 所以,當(dāng)時間需要精確的時候,覺得to_char還是必要的 7. 日期格式?jīng)_突問題 輸入的格式要看你安裝的ORACLE字符集的類型, 比如: US7ASCII, date格式的類型就是: 39。39。01Jan0139。39。 alter system set NLS_DATE_LANGUAGE = American alter session set NLS_DATE_LANGUAGE = American 或者在to_date中寫 select to_char(to_date(39。39。2002082639。39。,39。39。yyyymmdd39。39。),39。39。day39。39。,39。39。NLS_DATE_LANGUAGE = American39。39。) from dual。 注意我這只是舉了NLS_DATE_LANGUAGE,當(dāng)然還有很多, 可查看 select * from nls_session_parameters select * from V$NLS_PARAMETERS 8. select count(*) from ( select rownum1 rnum from all_objects where rownum = to_date(39。39。2002022839。39。,39。39。yyyymmdd39。39。) to_date(39。39。2002 020139。39。,39。39。yyyymmdd39。39。)+1 ) where to_char( to_date(39。39。2002020139。39。,39。39。yyyymmdd39。39。)+rnum1, 39。39。D39。39。 ) not in ( 39。39。139。39。, 39。39。739。39。 ) 查找20020228至20020201間除星期一和七的天數(shù) , 讓后將結(jié)果相減(得到的是1/100秒, 而不是毫秒). 9. select months_between(to_date(39。39。0131199939。39。,39。39。MMDDYYYY39。39。), to_date(39。39。1231199839。39。,39。39。MMDDYYYY39。39。)) MONTHS FROM DUAL。 1 select months_between(to_date(39。39。0201199939。39。,39。39。MMDDYYYY39。39。), to_date(39。39。1231199839。39。,39。39。MMDDYYYY39。39。)) MONTHS FROM DUAL。 10. Next_day的用法 Next_day(date, day) MondaySunday, for format code DAY MonSun, for format code DY 17, for format code D 11 select to_char(sysdate,39。39。hh:mi:ss39。39。) TIME from all_objects 注意:第一條記錄的TIME 與最后一行是一樣的 可以建立一個函數(shù)來處理這個問題 create or replace function sys_date return date is begin return sysdate。 end。 select to_char(sys_date,39。39。hh:mi:ss39。39。) from all_objects。 12.160