Oracle to_char的用法的描述
以下的文章主要是對(duì)Oracle to_char的用法的描述,相對(duì)而言O(shè)racle to_char能在很短的時(shí)間里被廣泛的應(yīng)用,說(shuō)明它的可實(shí)際應(yīng)用性還是占優(yōu)勢(shì)的,以下就是文章的具體內(nèi)容的介紹,希望你會(huì)有所收獲。
- The following are number examples for the to_char function.
- to_char(1210.73, '9999.9') would return '1210.7'
- to_char(1210.73, '9,999.99') would return '1,210.73'
- to_char(1210.73, '$9,999.00') would return '$1,210.73'
- to_char(21, '000099') would return '000021'
- The following is a list of valid parameters when
the to_char function is used to convert a date to a string.
These parameters can be used in many combinations.- Parameter Explanation
- YEAR Year, spelled out
- YYYY 4-digit year
- YYY
- YY
- Y Last 3, 2, or 1 digit(s) of year.
- IYY
- IY
- I Last 3, 2, or 1 digit(s) of ISO year.
- IYYY 4-digit year based on the ISO standard
- Q Quarter of year (1, 2, 3, 4; JAN-MAR = 1).
- MM Month (01-12; JAN = 01).
- MON Abbreviated name of month.
- MONTH Name of month, padded with blanks to length of 9 characters.
- RM Roman numeral month (I-XII; JAN = I).
- WW Week of year (1-53) where week 1 starts on the first day of the year and continues to the seventh day of the year.
- W Week of month (1-5) where week 1 starts on the first day of the month and ends on the seventh.
- IW Week of year (1-52 or 1-53) based on the ISO standard.
- D Day of week (1-7).
- DAY Name of day.
- DD Day of month (1-31).
- DDD Day of year (1-366).
- DY Abbreviated name of day.
- J Julian day; the number of days since January 1, 4712 BC.
- HH Hour of day (1-12).
- HH12 Hour of day (1-12).
- HH24 Hour of day (0-23).
- MI Minute (0-59).
- SS Second (0-59).
- SSSSS Seconds past midnight (0-86399).
- FF Fractional seconds.
- The following are date examples for the to_char function.
- to_char(sysdate, 'yyyy/mm/dd'); would return '2003/07/09'
- to_char(sysdate, 'Month DD, YYYY'); would return 'July 09, 2003'
- to_char(sysdate, 'FMMonth DD, YYYY'); would return 'July 9, 2003'
- to_char(sysdate, 'MON DDth, YYYY'); would return 'JUL 09TH, 2003'
- to_char(sysdate, 'FMMON DDth, YYYY'); would return 'JUL 9TH, 2003'
- to_char(sysdate, 'FMMon ddth, YYYY'); would return 'Jul 9th, 2003'
- You will notice that in some examples, the format_mask parameter
begins with "FM". This means that zeros and blanks are suppressed. This can be seen in the examples below.- to_char(sysdate, 'FMMonth DD, YYYY'); would return 'July 9, 2003'
- to_char(sysdate, 'FMMON DDth, YYYY'); would return 'JUL 9TH, 2003'
- to_char(sysdate, 'FMMon ddth, YYYY'); would return 'Jul 9th, 2003'
- The zeros have been suppressed so that the day component shows as "9" as opposed to "09".
Oracle函數(shù)to_char轉(zhuǎn)化數(shù)字型指定小數(shù)點(diǎn)位數(shù)的用法
Oracle to_char,函數(shù)功能,就是將數(shù)值型或者日期型轉(zhuǎn)化為字符型。
比如最簡(jiǎn)單的應(yīng)用:
- /*1.0123--->'1.0123'*/
- Select TO_CHAR(1.0123) FROM DUAL
- /*123--->'123'*/
- Select TO_CHAR(123) FROM DUAL
接下來(lái)再看看下面:
- /*0.123 ---> '.123' */
- SELEC TO_CHAR(0.123) FROM DUAL
上面的結(jié)果 '.123' 在大多數(shù)情況下都不是我們想要的結(jié)果,我們想要的應(yīng)該是 '0.123'。
我們來(lái)看一下Oracle to_char函數(shù)的具體用法:
- TO_CHAR ( n [, fmt [, 'nlsparam']] )
該函數(shù)將NUMBER類(lèi)型的n按數(shù)值格式fmt轉(zhuǎn)換成VARCHAR2類(lèi)型的值。'nlsparams'指定由數(shù)值格式的元素返回的字符,包括:
小數(shù)點(diǎn)字符
組分隔符
本地錢(qián)幣符號(hào)
國(guó)際錢(qián)幣符號(hào)
變?cè)男问綖椋?/p>
- 'NLS_NUMERIC_CHARACTERS="dg" NLS_CURRENCY="tcxt" NLS_ISO_CURRENCY=territory'
其中d為小數(shù)點(diǎn)字符,g為組分隔符。
例 :
- TO_CHAR (17145,'L099G999','NLS_NUMERIC_CHARACTERS=".," NLS_CURRENCY="NUD"')=NUD017,145
通過(guò)上面的了解,再查看fmt的一些格式,我們可以用以下表達(dá)式得到'0.123'的值:
- /*0.123 ---> ' 0.123' */
- Select TO_CHAR(0.123,'0.999') FROM DUAL
- /*100.12 ---> '######' */
- Select TO_CHAR(100.12,'0.999') FROM DUAL
- /*1.12 ---> ' 1.120' */
- Select TO_CHAR(1.12,'0.999') FROM DUAL
' 0.123'是出來(lái)了,可是前面又多了一個(gè)空格。
對(duì)于 100.12 的值卻是######,以及'1.12'的值變成了 '1.120'。
我們重新確定一個(gè)新的需求:
1、去空格
2、小數(shù)點(diǎn)最多4位,最少保留2位。
1--->'1.00';1.1--->'1.00';1.12-->'1.12';1.1234--->'1.1234';
1.12345--->'1.1235'
最終實(shí)現(xiàn)如下:
/*
FM :除空格
9999999.0099:允許小數(shù)點(diǎn)左邊最大正數(shù)為7位,小數(shù)點(diǎn)右邊最少2位,最多4位,且在第5位進(jìn)行四舍五入
- */
- Select TO_CHAR(123.0233,'FM9999999.0099') FROM DUAL
PLSQL小經(jīng)驗(yàn)一、 Oracle的to_char()函數(shù)功能很強(qiáng)大但是在用它格式化數(shù)值型數(shù)據(jù)時(shí)應(yīng)該請(qǐng)注意以下幾項(xiàng)。如果是小數(shù)如:0.23這樣的數(shù)據(jù)經(jīng)過(guò)to_char后再顯示會(huì)變?yōu)?23,如果你為了讓它顯示出原來(lái)的0.23的話(huà)就得用Oracle To_char(要格式化的數(shù)值,’0.999’)保留三個(gè)小數(shù),并顯示但這里就要注意了。
他為你截取小數(shù)的時(shí)候是四舍五入了。所以如果是要求截掉小數(shù)而不四舍五入的話(huà)就應(yīng)該自己寫(xiě)個(gè)函數(shù)截下去后再規(guī)格化。以保證它不四舍五入。
二、 To_char(1.9999,’FM90.0999’)這個(gè)函數(shù)規(guī)格化時(shí)90.0999的含義是有9的地方如果有數(shù)字就顯示如果沒(méi)有數(shù)字就不顯示,有0的地方在沒(méi)有數(shù)字的時(shí)候也會(huì)有0來(lái)占位.但這樣做也有一個(gè)很大的缺點(diǎn),就是如果是整數(shù)時(shí)它也仍然會(huì)顯示”.”,不要小瞧這個(gè)點(diǎn),一般來(lái)講頁(yè)面上要顯示的話(huà)這個(gè)點(diǎn)就是多余的.也給我們?cè)斐闪瞬恍〉穆闊?還要自己再寫(xiě)函數(shù)來(lái)把這個(gè)小點(diǎn)搞定.
三、 對(duì)于日期型的Oracle倒時(shí)提供了一個(gè)好的處理方法,可以把日期做成數(shù)值型的.然后再Oracle To_char就能顯示出你所需要的類(lèi)型了.
四、 在使用select into時(shí)一定要注意,這種方法你一定要確認(rèn)肯定會(huì)有數(shù)據(jù)被查出時(shí)才能使用.如果查詢(xún)結(jié)果為空時(shí)會(huì)導(dǎo)致報(bào)錯(cuò).還有一種情況是查出來(lái)的數(shù)據(jù)是多條也會(huì)報(bào)錯(cuò).所以應(yīng)該盡量便宜游標(biāo)來(lái)做.會(huì)減少錯(cuò)誤產(chǎn)生的機(jī)率.
五、 還有注意一點(diǎn)rownum不支持排序,就是說(shuō)你想用這個(gè)來(lái)控制行數(shù)的話(huà)就會(huì)發(fā)現(xiàn)他沒(méi)有按你指定的排序方式顯示.,這是一個(gè)很難辦的事.而且如果你用rownum=2這樣的語(yǔ)句來(lái)輸出第二行的話(huà)也是行不通的.
六、 最蹩腳的一點(diǎn)是Oracle對(duì)null的判斷變態(tài)到極點(diǎn).如果你說(shuō)某個(gè)變量 aa=null它是判斷不出來(lái)的.盡管aa的確是空.即使在選擇條件里也是判斷不出來(lái)的.不知道為什么,只好用nvl()這個(gè)函數(shù)來(lái)判斷了.在條件之外可以用 aa is null 來(lái)判斷.
補(bǔ)充一點(diǎn).就是在寫(xiě)存儲(chǔ)過(guò)程時(shí)要注意參數(shù)名不能與數(shù)據(jù)庫(kù)字段名相同.否則Oracle會(huì)把這個(gè)參數(shù)名看成是字段名的,即使你用表的別名區(qū)分也不行.所以起參數(shù)名的時(shí)候一定要注意這點(diǎn)了 。
【編輯推薦】