由於業務需要,把本來的varchar2轉換成了number(22,2),其中有一個欄位儲存的是0.xx型別的資料。問題來了,今天再次查詢發現資料庫中儲存的是0.01,但是查詢結果是.01,開始以為是程式碼問題,排查後發現問題在於bean中是string,在查詢對映轉換中把前面的0省略掉了(度娘說是儲存時為了節省空間,可是表裡會什麼沒有省略0呢?查詢做char轉換才省略?)
網上搜到了幾種解決辦法:
1)select to_char(rate) from mytest;


2)select case when rate like '.%' then '0'||rate else to_char(rate) end from mytest;


4) select to_char(rate,'fm9999990.9999') from mytest;

猜想:前面說的為什麼資料庫中顯示的是0.22,我覺得實際儲存應該 還是0.22,只是在查詢中做了隱是式轉換,程式碼底層把number類轉換成了string.通過substring函式擷取第一位是點不是0,可能是在subtrings時就已經先to_char把number轉了型別。。