Oracle Decode Function

Oracle Decode Function:

Oracle DECODE() function compares the first argument with the second argument. IF they are equal, the function returns the third argument otherwise return default value.

The Oracle/PLSQL DECODE function has the functionality of an IF-THEN-ELSE statement:

SELECT DECODE (1, 1, 'ONE', 'TWO') FROM DUAL;

Preview

  • Decode works like the following if statement:
begin
if 1=1 then
dbms_output.put_line('one');
else
dbms_output.put_line('two');
end if;
end;

Preview

  • If you want to specify a default value when the first argument is not equal to the second one, the default value to the argument list as shown below:
SELECT DECODE (1, 2, 'ONE', 'TWO') FROM DUAL;
Result: TWO
  • This example also work like if-else statement:
begin
if 1=2 then
dbms_output.put_line('one');
else
dbms_output.put_line('two');
end if;
end;
Result: TWO

You might also like:


comments powered by Disqus