title: Order by clause using case expression
body: <p>I am passing two parameters:</p>
  
  <p>@Column and @Direction, both of them are nvarchar(50)</p>
  
  <p>I want to change the order by of the select query from the values inside the parameters.</p>
  
  <p>For example, </p>
  
  <pre><code>if @Column = Name and @Direction = 'Desc' then order by Name desc
  if @Column = Name and @Direction = 'Asc'  then order by Name asc
  if @Column = Type and @Direction = 'Desc' then order by Type desc
  if @Column = Type and @Direction = 'Asc'  then order by Type asc
  </code></pre>
  
  <p>My table is called Cars.</p>
  
  <p>it has three columns:  CarId, Name, Type</p>
  
  <p>I tried to do this but it doesn't return the right result (as if order by is ignored)</p>
  
  <pre><code>select * from Cars
  order by        
  case 
  when @SortColumn = 'Type' and @SortDir = 'Desc' then 1
  when @SortColumn = 'Type' and @SortDir = 'Desc' then 1
  when @SortColumn = 'Name' and @SortDir = 'Asc'  then 3
  when @SortColumn = 'Name' and @SortDir = 'Asc'  then 3, end asc
  </code></pre>