Thursday, May 14, 2009

sql function : NVL can be used to provide a default value when a field is null.

if salary = Null
then salary = 0
else
salary = emp_salary from emp_table
end if

This can be reduced to
select nvl(emp_salary,0) from emp_table;

---------------------------------------------------------------------------

sql function : DECODE can be used instead of a nested if condition or a case statement.

DECODE(expression or column,if_value,then_return_this_value,[ if_value,then_return_this_value,...],default_return_value)

e.g.
Following sql will assign state codes for Washington or Ohio and default to Florida if the state is neither of the two.
select emp_id, decode(emp_state,'Washington','WA','Ohio','OH','FL')
from emp_table;

Following sql will assign tax percentage as 30 if salary >=60000 else 20.
select emp_id, decode(emp_tax_pct,emp_salary >= 60000,30,20)
from emp_table;

No comments:

Post a Comment