order by to return null values

  • I have 3 columns in one table.

    Col1, Col2, Col3.

    The data in these columns has data, and some null values. For Eg:

    Col1 Col2 Col3

    Data1 Data2 Data3

    null null null

    Data1a Data2a null

    Data1b null Data3a

    null Data2b Data3b

    I need to run a Select query, such that the results return with where all the 3 columns have null values, then atleast 1 or more null values, and then with data in all columns.

    Let me know how I can write a query to return data

    Thanks

  • select

    *

    from

    MyTable

    order by

    case

    when col1 is null and col2 is null and col3 is null

    then 1

    when col1 is null or col2 is null or col3 is null

    then 2

    else 3

    end

Viewing 2 posts - 1 through 1 (of 1 total)

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