FW: Microsoft SQL Server – Sort Values Ascending But NULLS Last

This is a frequent request in newsgroups and fora. People want to sort the column in ascending order but don’t want the NULLS at the beginning.Oracle has this syntax:


SQL Server does not have this. But there are 2 ways to do this. The first one is by using case and the second one by using COALESCE and the maximum value for the data type in the order by clause.

Read more at SQL Server Code,Tips and Tricks, Performance Tuning. These two methods will also work on Sybase ASE if you use a derived table or temp table instead of the memory table. 🙂

Share Button

2 Replies to “FW: Microsoft SQL Server – Sort Values Ascending But NULLS Last”

  1. BryanTheCrow over on Digg.com suggested:
    Easier way:

    ORDER BY IsNull(ColumnName, @ValueThatsBiggerThanEverythingElse)

    Just replace the @ValueThatsBiggerThanEverythingElse variable with what it says (I didn’t fill it in because it’d be different depending on the data type of your column). For DESC sorting, just make the value smaller than everything else.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.