title: T-SQL Conditional Order By
body: <p>I am trying to write a stored procedure that returns a list of object with the sort order and sort direction selected by the user and passed in as sql parameters.</p>
  
  <p>Lets say I have a table of products with the following columns: product_id(int), name(varchar), value(int), created_date(datetime)
  and parameters @sortDir and @sortOrder</p>
  
  <p>I want to do something like</p>
  
  <pre><code>select *
  from Product
    if (@sortOrder = 'name' and @sortDir = 'asc') 
    then order by name asc
    if (@sortOrder = 'created_date' and @sortDir = 'asc') 
    then order by created_date asc
    if (@sortOrder = 'name' and @sortDir = 'desc') 
    then order by name desc
    if (@sortOrder = 'created_date' and @sortDir = 'desc') 
    then order by created_date desc
  </code></pre>
  
  <p>I tried do it with case statements but was having problems since the data types were different. Anyone got any suggestions?</p>