Wednesday, May 19, 2010

What is Decode sql functions ?

Decode is the sql functions for the replacement of If- Then -else logic and case is similar to
If-Then-else logic except that we can make logical comparison of columns involved in the case
structures.
Ex: select case snum when snum > 10 then 'High' when snum>5 then 'Low' end from sales.

Ex: select decode(snum,10,'high',5,'low') from sales... that is we cannot make logical comparison of columns in Decode() functions.

Ex: SELECT NAME,SAL,DECODE(DEPTNO,10,'ACCOUNTING',20,'RESEARCH',30,'SALES',40,'OPERATIONS','OTHERS')
"DEPARTMENTS" FROM EMP;

A DECODE FUNCTION ALWAYS TAKES MINIMUM OF 4 ARGUMENTS
DECODE
IF N1=N2
THEN RETURN N3
ELSE
RETURN N4

No comments:

Post a Comment