Bala
Bala

Reputation: 1476

Decode function in Oracle

I have a condition like if(someparam!=value1) someparam=1 if(someparam!=value2) someparam =2 Default value = 1

How to use decode function for this condition

Upvotes: 3

Views: 1325

Answers (3)

Montse Garcia
Montse Garcia

Reputation: 427

DECODE(SomeParam, someParam!=value1, 1, someParam!=value2, 2, 1)

The default is the same as when assessing someparam! = Value1 therefore can simplify it like this:

DECODE(SomeParam, someParam!=value2, 2, 1)

I hope this helps :-)

Upvotes: 0

Thilo
Thilo

Reputation: 262514

 decode(someparam, value2, 1, 2)

The whole comparison to value1 seems redundant, as it is only going to the default value anyway.

Upvotes: 0

Michael Pakhantsov
Michael Pakhantsov

Reputation: 25370

DECODE(SomeParam, Value1, DECODE(SomeParam, Value2, 1, 2), 1)

but case is better:

case when someparam != Value1 then 1
    whene someparam != Value2 then 2
    else 1
end

Upvotes: 7

Related Questions