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