DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How to Recover a Deleted Table in a SQL Server Database
  • Restoring the MS SQL Server Database in Easy Steps
  • How To Convert MySQL Database to SQL Server
  • How To Fix SQL Database Restore Failed, Database Is in Use

Trending

  • Static and Dynamic Application Security Testing With IBM Appscan
  • MQTT, Kafka, NATS, or Zenoh? Choosing Edge-to-Cloud Messaging by Traffic Pattern
  • Building a Self-Healing Docker CI Gatekeeper With BuildKit
  • How to Format Articles for DZone

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Databases
  4. Dynamic Sorting in MS SQL Server

Dynamic Sorting in MS SQL Server

Let users sort their columns in MS SQL.

By 
NaveenKumar M
·
Oct. 09, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
12.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

What Is Sorting?

Sorting is a process of arranging items in a sequence by some standard or principle.

In MS SQL, the ORDER BY clause is used to arrange the data in ascending or descending order.

How Dynamic Sorting Works in MS SQL

Let's think of a web application that displays employee information in a grid with FirstName, LastName, Email, Title, Phone, and few other columns. On clicking the column heading the result set should sort accordingly.

Here, the sort column is dynamic, it depends on the user who clicks. To implement the sort on all the columns we use the CASE statement in the ORDER BY clause.

In the query below, @SortColumn defines the column that needs to be sorted, and @SortType defines ascending or descending. And the columns required for dynamic sorting are included in the CASEs under ORDER BY.

MS SQL
 
DECLARE @SortColumn AS VARCHAR(100) = 'firstname'
       ,@SortType   AS VARCHAR(100) = 'asc'

SELECT EmployeeKey,FirstName,LastName,Title,EmailAddress
FROM dbo.DimEmployee WITH(NOLOCK)
ORDER BY 
CASE WHEN @SortColumn = 'FirstName'      AND @SortType ='ASC'  THEN FirstName    END ASC,
CASE WHEN @SortColumn = 'FirstName'      AND @SortType ='DESC' THEN FirstName    END DESC,
CASE WHEN @SortColumn = 'LastName'       AND @SortType ='ASC'  THEN LastName     END ASC,
CASE WHEN @SortColumn = 'LastName'       AND @SortType ='DESC' THEN LastName     END DESC,
CASE WHEN @SortColumn = 'Title'          AND @SortType ='ASC'  THEN Title        END ASC,
CASE WHEN @SortColumn = 'Title'          AND @SortType ='DESC' THEN Title        END DESC,
CASE WHEN @SortColumn = 'EmailAddress'   AND @SortType ='ASC'  THEN EmailAddress END ASC,
CASE WHEN @SortColumn = 'EmailAddress'   AND @SortType ='DESC' THEN EmailAddress END DESC

In a real-time application, this logic can be written in the stored procedure which is called on clicking on the column which needs to be sorted, the respective column name, and the sort order to be passed as parameters to the stored procedure.

Microsoft SQL Server sql Sorting Database

Opinions expressed by DZone contributors are their own.

Related

  • How to Recover a Deleted Table in a SQL Server Database
  • Restoring the MS SQL Server Database in Easy Steps
  • How To Convert MySQL Database to SQL Server
  • How To Fix SQL Database Restore Failed, Database Is in Use

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook