Tuesday, March 28, 2023

Database - What is the use of Decode function in SQL?

In Oracle, Decode function allows us to add procedural if-then-else logic to the query. Decode compares the expression to each search value one by one, If expression is equal to a search, then the corresponding result is returned by the Oracle database. If a match is not found , then the default is returned.


Example:

Select bank_name, decode(bank_id l, 001, 'SBI', 002, 'ICICI',003,'Axis',Gateway) result from banks;

This above statement is equivalent to if-then-else statement as below:

IF bank_id = 001 THEN

result := 'SBI';

ELSIF bank_id = 002 THEN

result :='ICICI';

ELSIF bank_id =003 THEN

result := 'Axis';

ELSE

result := 'Gateway';

END IF


No comments:

Post a Comment

Featured Post

11g to 12c OSB projects migration points

1. Export 11g OSB code and import in 12c Jdeveloper. Steps to import OSB project in Jdeveloper:   File⇾Import⇾Service Bus Resources⇾ Se...