Dynamic ORDER BY in SQL Server using CASE WHEN

Few times I have observed that developers directly consider the dynamic SQL option as a solution, when there is need to sort the result set on basis of user input variables/parameters. Let’s see how can we do the sorting using case when.

Note: As you can notice, we had to write separate case when statements for every possibilities because the result set are of different types.

Prasad Sahoo

Prasad Sahoo, is an enthusiast of Microsoft technologies in general and a passionate database professional, mainly focusing on SQL Server performance tuning and business intelligence. He has contributed a long time of his career working on SQL Server and other RDBMS. If he is not busy with SQL stuff then he must be watching some Hollywood movies.

Leave a Reply

%d bloggers like this: