close

 

NVL (expr1, expr2)->expr1為NULL,返回expr2;不為NULL,返回expr1。註意兩者的類型要一致

NVL2 (expr1, expr2, expr3) ->expr1不為NULL,返回expr2;為NULL,返回expr3。expr2和expr3類型不同的話,expr3會轉換為expr2的類型

NULLIF (expr1, expr2) ->相等返回NULL,不等返回expr1

COALESCE (eXPression_1, expression_2, ...,expression_n)
列表中第一個非空的表達式是函數的返回值,假如所有的表達式都是空值,最終將返回一個空值。
NVL進階版函式
select Coalesce('C','B','A') from dual ==> C
select Coalesce(null,'B','A') from dual ==> B
select Coalesce(null,null,'A') from dual ==> A

 

DECODE用法:

DECODE(value,if1,then1,if2,then2,if3,then3,……,else),表示如果value 等于if1?,DECODE函數返回then1,……

select decode( x , 1 , ‘x is 1 ‘, 2 , ‘x is 2 ‘, ‘others’) from dual

當x等於1時,則返回‘x is 1’。

當x等於2時,則返回‘x is 2’。

否則,返回others’。

在需要比較2個值的時候,我們可以配合SIGN()函數一起使用。

SELECT DECODE( SIGN(5 -6), 1 ‘Is Positive’, -1, ‘Is Nagative’, ‘Is Zero’)

同樣,也可以用CASE實現:

SELECT CASE SIGN(5 – 6)

WHEN 1 THEN ‘Is Positive’

WHEN-1 THEN ‘Is Nagative’

ELSE’Is Zero’ END

FROM DUAL

另外,大家還可以在Order by中使用Decode。

arrow
arrow
    全站熱搜

    kunchung 發表在 痞客邦 留言(0) 人氣()