sql order by with case


/ Published in: SQL
Save to your folder(s)



Copy this code and paste it in your HTML
  1. DECLARE @SortOrder tinyint
  2. SET @SortOrder = 2
  3.  
  4. SELECT CompanyName,
  5. ContactName,
  6. ContactTitle
  7. FROM Customers
  8. ORDER BY CASE WHEN @SortOrder = 1 THEN CompanyName
  9. WHEN @SortOrder = 2 THEN ContactName
  10. ELSE ContactTitle
  11. END

URL: http://www.sqlteam.com/article/dynamic-order-by

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.