Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

Any simpler way to avoid case

select case when d.arn_no is not null then to_char(d.arn_no)
                                                                     when d.boe_no is not null then d.boe_no
                                                                     else d.do_no end
from docs d

Is there any other simpler way without using case.?
0
sakthikumar
Asked:
sakthikumar
  • 2
4 Solutions
 
slightwv (䄆 Netminder) Commented:
Looks like you are looking to find the first not null in a list of values.

If so, try coalesce:

coalesce(d.arn_no ,d.boe_no, d.do_no)
0
 
sdstuberCommented:
case seems like the simplest way but if you don't want to use it, you could try nvl2 and nvl

select nvl2(d.arn_no,nvl(d.boe_no,d.do_no),to_char(d.arn_no)) from docs d
0
 
johnsoneSenior Oracle DBACommented:
I believe that this will do the same thing:

nvl2(d.arn_no, to_char(d.arn_no), nvl2(d.boe_no, d.boe_no, d.do_no))
0
 
sdstuberCommented:
if you want to use coalesce, then all of the parameters must be of the compatible types.
i.e.  all numbers or all strings

based on your original code, it seems like arn_no is a number and the others are strings.
if so then try this...

coalesce(to_char(d.arn_no) ,d.boe_no, d.do_no)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now