How to conditionally select a column in an Oracle query

I want to do something like:

select (if lookup = 8 then 08 else lookup) lookup , //more columns from lookup_table order by lookup 

Unfortunately, Oracle does not seem to be like this syntax, and I cannot figure out why or find which other function I should use.

Basically, if the search value is 8, I want to get 08, otherwise I want the search value. I'm sure I'm just stupid.

+4
source share
1 answer

You need a case statement:

 select (case when lookup = 8 then 8 else lookup end) as lookup 

If lookup is a character string, you probably want:

 select (case when lookup = '08' then '08' else lookup end) as lookup 

If lookup is an integer, and you want to convert it to a string, then:

 select (case when lookup = 8 then to_char(lookup, '00') else to_char(lookup, '00') end) as lookup 

However, this would seem unnecessary for me.

+11
source

All Articles