Posted By

samwa on 03/18/11


Tagged

sql order


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

samwa


sql order by with case


 / Published in: SQL
 

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

  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

Report this snippet  

You need to login to post a comment.