Using Expressions for SORT Order in SQL Server

You might want to sort the results of an query in a different order from the traditional sort (Ascending/Descending) order.

For example , you wish to sort the Department records by GroupName and force the GroupName “Manufacturing” to be sorted first.

How to use expression to sort records in SQL Server ?

You could do this by using the expression which would provide the sort order that you require.

Here’s a sample query demonstrating how this can be done.

Use AdventureWorks2014
GO
SELECT * from HumanResources.Department
ORDER BY CASE GroupName   
WHEN 'Manufacturing' THEN NULL      
ELSE GroupName      
END;  

SNAGHTML8450788

Leave A Reply

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

You May Also Like

When dealing with a relational database management system (RDBMS) like SQL Server, compatibility level is an important concept to understand....
In this blog post, let’s learn about the error message “49975 – Unable to load controller client certificate due to...
In this blog post, let’s learn about the error message “49973 – Cannot remove tempdb remote file to local tempdb...