1. 程式人生 > >Oracle中欄位為空處理成0

Oracle中欄位為空處理成0

Oracle資料庫中的函式


A :表名, B: 要修改的欄位名

update A set  B = replace(b, 'null', '0') where id = '5644';


執行效果如下:


decode函式的用法

作用是--顯示的時候變了  資料庫真實資料不變啊

含義解釋:
decode(條件,值1,返回值1,值2,返回值2,...值n,返回值n,預設值)

if (條件==值1)

 then    

return(翻譯值1)

elsif (條件==值2)

then    

return(翻譯值2)    

......

elsif (條件==值n)

 then    

return(翻譯值n)

else    

return(預設值)

end if

注:其中預設值可以是你要選擇的column name 本身,也可以是你想定義的其他值,比如Other等;

舉例說明:

現定義一table名為output,其中定義兩個column分別為monthid(var型)和sale(number型),若sale值=1000時翻譯為D,=2000時翻譯為C,=3000時翻譯為B,=4000時翻譯為A,如是其他值則翻譯為Other;

SQL如下:

Select monthid , decode (sale,1000,'D',2000,'C',3000,'B',4000,'A',’Other’) sale from output

特殊情況:

若只與一個值進行比較

Select monthid ,decode(sale, NULL,‘---’,sale) sale from output

另:decode中可使用其他函式,如nvl函式或sign()函式等;

NVL(EXPR1,EXPR2)

若EXPR1是NULL,則返回EXPR2,否則返回EXPR1.

SELECT NAME,NVL(TO_CHAR(COMM),'NOT APPLICATION') FROM TABLE1;

如果用到decode函式中就是

select monthid,decode(nvl(sale,6000),6000,'NG','OK') from output

sign()函式根據某個值是0、正數還是負數,分別返回0、1、-1,

如果取較小值就是

select monthid,decode(sign(sale-6000),-1,sale,6000) from output,即達到取較小值的目的。