Wednesday, August 10, 2005

Some useful Oracle functions

nvl(expr1, expr2)
if expr1 is null, return expr2. Otherwise, return expr1
nvl(select * from bs_table, 'No data in table')
nvl2(expr1, expr2,expr3)
if expr1 is null, return expr2. Otherwise, return expr3.
nvl2(select * from bs_table where bsname='Bob', 'Bob has no records', 'Records found for Bob')

decode(expr, result1, replace1, result2, replace2, ... [default])
Evaluate expr, if it equals result1, return replace1 instead, etc. If it doesn't match, return default. If no default given, return null.
SELECT DECODE(mydatacolumn,
'TRUE', 'yes',
'FALSE', 'no',
'n/a')

FROM vmyview b WHERE
b.someid=62 AND b.otherid=2516 AND b.personid=20
AND ROWNUM<2
RTRIM(expr, string)
Trims the contents of string from the end of expr. You can use this like NVL with more complicated expressions.
RTRIM('Bscolumn: ' || select bscolumn from bstable, 'Bscolumn: ')
Will return Bscolumn: <query results here> or null.

No comments:

Labels

Blog Archive

Contributors