nulls first | last

  • In Oracle and DB2, one can specify how nulls are sorted in the order by clause. e.g.

    select ... order by ... desc nulls first(last)

    This doesn't seem to be availiabe in SQL Server, is there any way I can control how nulls are sorted?

    Tim

  • You can use ISNULL in the order by.  If you're ordering a column containing values 1,2,3,4... you'd use ISNULL(column, 0) to put them at the top or ISNULL(column, 99999) to put them at the bottom.

  • Yeah , unfortunately Null sorting is not implemented in SQL Server.

     

    Another option you can use especially around issues where you don't have an open values is to use case.

    ORDER BY

    (CASE WHEN col IS NULL THEN 0 ELSE 1 END) DESC, col

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

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