Oracle時間與字符串的轉換

0.日期時間格式Year:yy two digits 兩位年 顯示值:07yyy three digits 三位年 顯示值:007yyyy four digits 四位年 顯示值:2007 git

Month:mm number 兩位月 顯示值:11mon abbreviated 字符集表示 顯示值:11月,如果英文版,顯示novmonth spelled out 字符集表示 顯示值:11月,如果英文版,顯示november 數據庫

Day:dd number 當月第幾天 顯示值:02ddd number 當年第幾天 顯示值:02dy abbreviated 當週第幾天簡寫 顯示值:星期五,如果英文版,顯示friday spelled out 當週第幾天全寫 顯示值:星期五,如果英文版,顯示fridayddspth spelled out, ordinal twelfth session

Hour:hh two digits 12小時進制 顯示值:01hh24 two digits 24小時進制 顯示值:13 oracle

Minute:mi two digits 60進制 顯示值:45 函數

Second:ss two digits 60進制 顯示值:25 spa

其它Q digit 季度 顯示值:4WW digit 當年第幾周 顯示值:44W digit 當月第幾周 顯示值:1 code

1. 日期和字符轉換函數用法(to_date,to_char) orm

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; //獲取時間的秒 three

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

2. 獲得一個數字的拼寫--0000-0-0select to_date(2222,'J') from dual;

--TO_CHAR(aDate,'JSP')能夠獲得一個數字的拼寫,TWO MILLION FOUR HUNDRED FIFTY-SIX THOUSAND FOUR HUNDRED

SEVENTY-TWOSELECT TO_CHAR(SYSDATE, 'JSP') FROM DUAL;

--Two Hundred Twenty-Twoselect 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 table1UNIONselect 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 = Americanalter session set NLS_DATE_LANGUAGE = American或者在to_date中寫select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual;注意我這只是舉了NLS_DATE_LANGUAGE,固然還有不少,可查看select * from nls_session_parametersselect * from V$NLS_PARAMETERS

8.select count(*)from ( select rownum-1 rnumfrom all_objectswhere rownum <= to_date('2002-02-28','yyyy-mm-dd') - to_date('2002-02-01','yyyy-mm-dd')+1)where to_char( to_date('2002-02-01','yyyy-mm-dd')+rnum-1, 'D' )not in ( '1', '7' )

查找2002-02-28至2002-02-01間除星期一和七的天數在先後分別調用DBMS_UTILITY.GET_TIME, 讓後將結果相減(獲得的是1/100秒, 而不是毫秒).

9. 查找月份select months_between(to_date('01-31-1999','MM-DD-YYYY'),to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM

DUAL;1select months_between(to_date('02-01-1999','MM-DD-YYYY'),to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM

DUAL;1.03225806451613

10. Next_day的用法Next_day(date, day)

Monday-Sunday, for format code DAYMon-Sun, for format code DY1-7, for format code D

11select to_char(sysdate,'hh:mi:ss') TIME from all_objects注意:第一條記錄的TIME 與最後一行是同樣的能夠創建一個函數來處理這個問題create or replace function sys_date return date isbeginreturn sysdate;end;

select to_char(sys_date,'hh:mi:ss') from all_objects;

12.得到小時數extract()找出日期或間隔值的字段值SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 2:38:40') from offerSQL> select sysdate ,to_char(sysdate,'hh') from dual;

SYSDATE TO_CHAR(SYSDATE,'HH')-------------------- ---------------------2003-10-13 19:35:21 07

SQL> select sysdate ,to_char(sysdate,'hh24') from dual;

SYSDATE TO_CHAR(SYSDATE,'HH24')-------------------- -----------------------2003-10-13 19:35:21 19

13.年月日的處理select older_date,newer_date,years,months,abs(trunc(newer_date-add_months( older_date,years*12+months ))) days

from ( selecttrunc(months_between( newer_date, older_date )/12) YEARS,mod(trunc(months_between( newer_date, older_date )),12 ) MONTHS,newer_date,older_datefrom (select hiredate older_date, add_months(hiredate,rownum)+rownum newer_datefrom emp))

14.處理月份天數不定的辦法select to_char(add_months(last_day(sysdate) +1, -2), 'yyyymmdd'),last_day(sysdate) from dual

16.找出今年的天數select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dual

閏年的處理方法to_char( last_day( to_date('02' | | :year,'mmyyyy') ), 'dd' )若是是28就不是閏年

17.yyyy與rrrr的區別'YYYY99 TO_C------- ----yyyy 99 0099rrrr 99 1999yyyy 01 0001rrrr 01 2001

18.不一樣時區的處理select to_char( NEW_TIME( sysdate, 'GMT','EST'), 'dd/mm/yyyy hh:mi:ss') ,sysdatefrom dual;

19.5秒鐘一個間隔Select TO_DATE(FLOOR(TO_CHAR(sysdate,'SSSSS')/300) * 300,'SSSSS') ,TO_CHAR(sysdate,'SSSSS')from dual

2002-11-1 9:55:00 35786SSSSS表示5位秒數

20.一年的第幾天select TO_CHAR(SYSDATE,'DDD'),sysdate from dual

310 2002-11-6 10:03:51

21.計算小時,分,秒,毫秒selectDays,A,TRUNC(A*24) Hours,TRUNC(A*24*60 - 60*TRUNC(A*24)) Minutes,TRUNC(A*24*60*60 - 60*TRUNC(A*24*60)) Seconds,TRUNC(A*24*60*60*100 - 100*TRUNC(A*24*60*60)) mSecondsfrom(selecttrunc(sysdate) Days,sysdate - trunc(sysdate) Afrom dual)

select * from tabnameorder by decode(mode,'FIFO',1,-1)*to_char(rq,'yyyymmddhh24miss');

//floor((date2-date1) /365) 做爲年floor((date2-date1, 365) /30) 做爲月d(mod(date2-date1, 365), 30)做爲日.

23.next_day函數 返回下個星期的日期,day爲1-7或星期日-星期六,1表示星期日next_day(sysdate,6)是從當前開始下一個星期五。後面的數字是從星期日開始算起。1 2 3 4 5 6 7日 一 二 三 四 五 六

---------------------------------------------------------------

select (sysdate-to_date('2003-12-03 12:55:45','yyyy-mm-dd hh24:mi:ss'))*24*60*60 from ddual日期 返回的是天 而後 轉換爲ss

24,round[舍入到最接近的日期](day:舍入到最接近的星期日)select sysdate S1,round(sysdate) S2 ,round(sysdate,'year') YEAR,round(sysdate,'month') MONTH ,round(sysdate,'day') DAY from dual

25,trunc[截斷到最接近的日期,單位爲天] ,返回的是日期類型select sysdate S1,trunc(sysdate) S2, //返回當前日期,無時分秒trunc(sysdate,'year') YEAR, //返回當前年的1月1日,無時分秒trunc(sysdate,'month') MONTH , //返回當前月的1日,無時分秒trunc(sysdate,'day') DAY //返回當前星期的星期天,無時分秒from dual

26,返回日期列表中最晚日期select greatest('01-1月-04','04-1月-04','10-2月-04') from dual

27.計算時間差注:oracle時間差是以天數爲單位,因此換算成年月,日

select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))/365) as spanYears from dual

//時間差-年select ceil(moths_between(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))) as spanMonths from dual

//時間差-月select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))) as spanDays from dual //時

間差-天select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24) as spanHours from dual

//時間差-時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 //時間差-分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 //時間差-秒

28.更新時間注:oracle時間加減是以天數爲單位,設改變量爲n,因此換算成年月,日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 //改變時間-年select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),add_months(sysdate,n) as newTime from dual //改變時間-月select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),to_char(sysdate+n,'yyyy-mm-dd hh24:mi:ss') as newTime from dual //

改變時間-日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 //改變時間-時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 //改變時間-分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 //改變時間-秒

29.查找月的第一天,最後一天SELECT Trunc(Trunc(SYSDATE, 'MONTH') - 1, 'MONTH') First_Day_Last_Month,Trunc(SYSDATE, 'MONTH') - 1 / 86400 Last_Day_Last_Month,Trunc(SYSDATE, 'MONTH') First_Day_Cur_Month,LAST_DAY(Trunc(SYSDATE, 'MONTH')) + 1 - 1 / 86400 Last_Day_Cur_MonthFROM dual;

30.TO_CHAR 是把日期或數字轉換爲字符串TO_DATE 是把字符串轉換爲數據庫中得日期類型轉換函數TO_NUMBER 將字符轉化爲數字

TO_CHAR使用TO_CHAR函數處理數字TO_CHAR(number, '格式')TO_CHAR(salary,’$99,999.99’);使用TO_CHAR函數處理日期TO_CHAR(date,’格式’);

TO_NUMBER使用TO_NUMBER函數將字符轉換爲數字TO_NUMBER(char[, '格式'])

TO_DATE使用TO_DATE函數將字符轉換爲日期TO_DATE(char[, '格式'])

數字格式格式9 表明一個數字0 強制顯示0$ 放置一個$符L 放置一個浮動本地貨幣符. 顯示小數點, 顯示千位指示符

相關文章
相關標籤/搜索