Oracle的資料類型轉換
Oracle的資料類型轉換分為顯式資料類型轉換和隱式類型轉換
在通常情況下,隱式資料類型轉換是可用的,但建議你做顯示資料類型轉換以確保SQL的可靠性.
顯式的資料類型轉換包括:
to_number 字元轉換為數字
to_date 字元轉換為日期
to_char 數字轉化為字元
to_char 日期轉化為字元TO_CHAR(date,'format_model')
必須加單引號,並且區分大小寫
能夠包含任一有效日期格式元素
有一個fm元素用來刪除填補的空,或者前置字元為零
用一個逗號與日期值分開select employee_id,to_char(hire_date,'MM/YY') as month
from employees
where last_name='Higgins'
employee_id month
205 06/94
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss am') as today from dual
2006-06-24 13:39:06 下午select to_char(to_date('21-1月-95'),'yyyy-mm-dd hh24:mi:ss am') as today from dual
1995-01-21 00:00:00 上午select to_char(sysdate,'dd "of" month') from dual
24 of 6月select to_char(syadate,'yyyy-mm-dd cc') from dual
2006-06-24 21
cc:世紀select to_char(sysdate,'yyy-mm-dd cc') as today from dual
006-06-24 21
select to_char(sysdate,'yy-mm-dd cc') as today from dual
06-06-24 21
select to_char(sysdate,'y,yyy-mm-dd cc') as today from dual
2,006-06-24 21
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函數to_char轉化數字型指定小數點位元的用法
to_char,函數功能,就是將數值型或者日期型轉化為字元型。
比如最簡單的應用:
Select
TO_CHAR(1.0123) FROM DUAL
Select TO_CHAR(123) FROM DUAL
接下來再看看下面:
SELEC
TO_CHAR(0.123) FROM DUAL
上面的結果 '.123' 在大多數情況下都不是我們想要的結果,我們想要的應該是 '0.123'。
我們來看一下to_char函數的具體用法:
TO_CHAR ( n [, fmt [, 'nlsparam']] )
該函數將NUMBER類型的n按數值格式fmt轉換成VARCHAR2類型的值。'nlsparams'指定由數值格式的元素返回的字元,包括:
.小數點字元
.組分隔字元
.本地錢幣符號
.國際錢幣符號
變元的形式為:
'NLS_NUMERIC_CHARACTERS="dg" NLS_CURRENCY="tcxt" NLS_ISO_CURRENCY=territory'
其中d為小數點字元,g為組分隔字元。
例 :TO_CHAR (17145,'L099G999','NLS_NUMERIC_CHARACTERS=".," NLS_CURRENCY="NUD"')=NUD017,145
通過上面的瞭解,再查看fmt的一些格式,我們可以用以下運算式得到'0.123'的值:
Select
TO_CHAR(0.123,'0.999') FROM DUAL
Select TO_CHAR(100.12,'0.999') FROM DUAL
Select TO_CHAR(1.12,'0.999') FROM DUAL
' 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'
最終實現如下:
Select
TO_CHAR(123.0233,'FM9999999.0099') FROM DUAL
PLSQL小經驗一、 Oracle的to_char()函數功能很強大但是在用它格式化數值型資料時應該請注意以下幾項。如果是小數如:0.23這樣的資料經過to_char後再<strong onmouseover="isShowAds = true;isShowAds2 = true;ads.Move(this,"http://rad.17luntan.com/UploadImage/17/633111011619218750.gif","%u89C1%u6240%u672A%u89C1%uFF01%u62A2%u8D2DLG%u9510%u6BD4%u6DB2%u6676%uFF0C%u8D60%u65E0%u7EBF%u5927%u793C%21","42887","顯示","%u663E%u793A%20","http%3A//210.192.100.35/11779028996160http%3A//www.lgemonitor.com.cn/mnt/index.html%3F11779028996160", event)" style="FONT-WEIGHT: normal; CURSOR: hand; COLOR: #0000ff; TEXT-DECORATION: underline" onclick="javascript:window.open("http://s0.17luntan.com/ClickPortal/WebClick.aspx?id=42887&k=%u663E%u793A&siteid=95d6d193-1fb9-4fc0-8708-b7ceb3276924&url=http%3A//wisebo.bokee.com/viewdiary.15801227.html&gourl=http%3A//210.192.100.35/11779028996160http%3A//www.lgemonitor.com.cn/mnt/index.html%3F11779028996160&parm=C794E0A4A49DF5F353EA88008FD215212B465427A50DDCDB&alliedsiteid=148");" onmouseout="isShowAds = false;isShowAds2 = false">顯示會變為.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會把這個參數名看成是欄位名的,即使你用表的別名區分也不行.所以起參數名的時候一定要注意這點了.