title: How can I run a query to sort by a column and asc/desc using parameters?
body: <p>I am working off this example.</p>
  
  <p><a href="http://www.drury.net.nz/2005/04/15/specifying-a-sort-parameter-for-a-tsql-stored-procedure/" rel="nofollow noreferrer">http://www.drury.net.nz/2005/04/15/specifying-a-sort-parameter-for-a-tsql-stored-procedure/</a></p>
  
  <pre><code>CREATE PROCEDURE getEmployees ( @ColumnName varchar(100) ) 
  AS 
    SELECT 
      EmployeeID, 
      FirstName, 
      LastName, 
      SSN, 
      Salary 
    FROM 
       Employees 
    ORDER BY 
      CASE 
        WHEN @ColumnName=’LastName’ THEN LastName 
        WHEN @ColumnName=’Salary’ THEN CONVERT(varchar(50), Salary) 
        WHEN @ColumnName=’SSN’ THEN SSN 
      END
  </code></pre>
  
  <p>The case statement works, but what if I have the following parameters: @SortColumn, @SortDirection.</p>
  
  <p>The @SortColumn could be any column of any type and it seems to use the case statement you have to convert the values to the same type. I suppose I can make them all VARCHAR and just make sure values like DateTime are put in the proper order to sort as I want.</p>
  
  <p>But what if I have the @SortDirection parameter set as ASC or DESC as a VARCHAR value? How can I adjust the query to do change the sort direction?</p>