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
YLast 3, 2, or 1 digit(s) of year. IYY
IY
ILast 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函數to_char轉化數字型指定小數點位數的用法
to_char,函數功能,就是將數值型或者日期型轉化為字符型。
比如最簡單的應用:
接下來再看看下面:
上面的結果 '.123' 在大多數情況下都不是我們想要的結果,我們想要的應該是 '0.123'。
我們來看一下to_char函數的具體用法:
TO_CHAR ( n [, fmt [, 'nlsparam']] )
通過上面的了解,再查看fmt的一些格式,我們可以用以下表達式得到'0.123'的值:
' 0.123'是出來了,可是前面又多了一個空格。
對于 100.12 的值卻是######,以及'1.12'的值變成了 '1.120'。
我們重新確定一個新的需求:
1、去空格
2、小數點最多4位,最少保留2位。
1--->'1.00';1.1--->'1.00';1.12-->'1.12';1.1234--->'1.1234';
1.12345--->'1.1235'
最終實現如下:
PLSQL小經驗
一、 Oracle的to_char()函數功能很強大但是在用它格式化數值型數據時應該請注意以下幾項。如果是小數如:0.23這樣的數據經過to_char后再顯示會變為.23,如果你為了讓它顯示出原來的0.23的話就得用To_char(要格式化的數值,’0.999’)保留三個小數,并顯示但這里就要注意了。他為你截取小數的時候是四舍五入了。所以如果是要求截掉小數而不四舍五入的話就應該自己寫個函數截下去后再規格化。以保證它不四舍五入。
二、 To_char(1.9999,’FM90.0999’)這個函數規格化時90.0999的含義是有9的地方如果有數字就顯示如果沒有數字就不顯示,有0的地方在沒有數字的時候也會有0來占位.但這樣做也有一個很大的缺點,就是如果是整數時它也仍然會顯示”.”,不要小瞧這個點,一般來講頁面上要顯示的話這個點就是多余的.也給我們造成了不小的麻煩.還要自己再寫函數來把這個小點搞定.
三、 對于日期型的Oracle倒時提供了一個好的處理方法,可以把日期做成數值型的.然后再To_char就能顯示出你所需要的類型了.
四、 在使用select into時一定要注意,這種方法你一定要確認肯定會有數據被查出時才能使用.如果查詢結果為空時會導致報錯.還有一種情況是查出來的數據是多條也會報錯.所以應該盡量便宜游標來做.會減少錯誤產生的機率.
五、 還有注意一點rownum不支持排序,就是說你想用這個來控制行數的話就會發現他沒有按你指定的排序方式顯示.,這是一個很難辦的事.而且如果你用rownum=2這樣的語句來輸出第二行的話也是行不通的. 六、 最惡心的一點是Oracle對null的判斷變態到極點.如果你說某個變量 aa=null它是判斷不出來的.盡管aa的確是空.即使在選擇條件里也是判斷不出來的.不知道為什么,只好用nvl()這個函數來判斷了.在條件之外可以用 aa is null 來判斷.
六 補充一點.就是在寫存儲過程時要注意參數名不能與數據庫字段名相同.否則Oracle會把這個參數名看成是字段名的,即使你用表的別名區分也不行.所以起參數名的時候一定要注意這點了.
只有注冊用戶登錄后才能發表評論。 | ||
![]() |
||
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
|
||
相關文章:
|
||