Ordering with nulls

  • I've the results of a full outer join that I want ordered. This is just an example as I'm struggling to get my head around it.

    create table TestTable

    (

    ColA nvarchar(50) null,

    ColB nvarchar(50) null

    )

    insert TestTable (ColA, ColB)

    select 'W', 'W'

    union all

    select 'X', 'W'

    union all

    select 'Y', 'Y'

    union all

    select 'Z', 'Z'

    union all

    select null, 'W'

    union all

    select null, 'X'

    union all

    select null, 'Y'

    union all

    select null, 'Z'

    union all

    select 'W', null

    union all

    select 'X', null

    union all

    select 'Y', null

    union all

    select 'Z', null

    I want it ordered with all the Ws together etc. with no nulls first, followed by nulls in ColA and then nulls in ColB:

    WW

    NULLW

    WNULL

    XX

    NULLX

    XNULL

    YY

    NULLY

    YNULL

    ZZ

    NULLZ

    ZNULL

  • How about this ?

    select ColA, ColB

    from TestTable

    order by isnull( ColA , ColB) , isnull( ColB ,ColA )

  • This will be more robust:

    select ColA, ColB

    from TestTable

    order by isnull( ColA , ColB) ,

    case when ColA is not null and ColB is not null then 1

    when ColA is null then 2

    when ColB is null then 3

    end

  • Nice shot, CC. You got there seconds before me 😉

    select * from TestTable

    order by

    ISNULL(ColA, ColB) + ISNULL(ColB, ColA)

    ______________________________________________________________________________
    How I want a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.

  • toddasd (8/31/2011)


    Nice shot, CC. You got there seconds before me 😉

    😀

  • ColdCoffee (8/31/2011)


    This will be more robust:

    select ColA, ColB

    from TestTable

    order by isnull( ColA , ColB) ,

    case when ColA is not null and ColB is not null then 1

    when ColA is null then 2

    when ColB is null then 3

    end

    Not just robust, but correct. The other solutions are not in the right order.

    ______________________________________________________________________________
    How I want a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.

  • That seems to do it. I'd experimented with case and isnull but never got as far as using them using together. Thanks for this!

  • toddasd (8/31/2011)


    ColdCoffee (8/31/2011)


    This will be more robust:

    select ColA, ColB

    from TestTable

    order by isnull( ColA , ColB) ,

    case when ColA is not null and ColB is not null then 1

    when ColA is null then 2

    when ColB is null then 3

    end

    Not just robust, but correct. The other solutions are not in the right order.

    YEah, the first query we both posted gave ordered result, but on large sets, we could expect mis-orders. This one will nail it.

  • zapouk (8/31/2011)


    That seems to do it. I'd experimented with case and isnull but never got as far as using them using together. Thanks for this!

    You're welcome. Thanks for setting up the questions; i dint take the pain of creating sample data, insert into tables blah blah.. so pat on your shoulder for a neatly-presented question.

  • Seconded. A good setup gets your question answered quickly. With tested code! 😎

    ______________________________________________________________________________
    How I want a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.

Viewing 10 posts - 1 through 9 (of 9 total)

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