coalesce is not working

  • Hi,

    I have the following table with some sample values, I want to return the first non null value in that order. COALESCE does not seem to work for me, it does not return the 3rd record. I need to include this in my select statement. Any urgent help please.

    Mobile    Business     Private

    NULL        345           NULL

    4646        65464        65765

    NULL                        564

    654654     564           6546

    I want the following as my results:

    Number

    345

    4646

    564

    654654

    Select COALESCE(Mobile,Business,Private) as Number  from Table returns:

    345

    4646

    654654

    select mobile,business,private where private is not null returns:

    65765

    564

    6546

    So is not nulls seems to bring up the 3rd record but then how do i include this in a case statement to return any 3.

    thanks

     

  • Try this

    Select

    COALESCE(CASE RTRIM(Mobile) WHEN '' THEN null ELSE Mobile END ,

    CASE RTRIM(Business) WHEN '' THEN null ELSE Business END,

    CASE

    RTRIM(Private) WHEN '' THEN null ELSE Private END) as Number from coalescetest

  • perfect, that's exactly what I was looking for.

    Thanks alot

  • Try,

    SELECT COALESCE(NULLIF(Mobile,''),NULLIF(Business,''),NULLIF(Private,'')) as Number

    FROM Table

    Andy

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply