title: Arithmetic overflow error When sorting by [TimeStamp] (column name)
body: <p>I'm getting the error:</p>
  
  <p><code>Arithmetic overflow error converting expression to data type smalldatetime</code></p>
  
  <p>Please note that I've already searched this topic, but my bug is unique because it is caused by the <code>ORDER BY</code> clause. the problematic line(s) is
  <code>WHEN 'TimeStamp' THEN [MyTable].[TimeStamp]</code>.</p>
  
  <p>when I comment those <code>TimeStamp</code>, the query executed successfully, but I still need a solution for sorting my <code>TimeStamp</code> column (renaming the column is not possible).</p>
  
  <p>This is my query:</p>
  
  <pre><code>SELECT
      Id,
      [TimeStamp],
      StatusCode
  FROM 
      (
      SELECT 
          TOP (@EndRecord) ROW_NUMBER() OVER 
          (       
          ORDER BY
              CASE @SortDirection
                  WHEN 'Desc' THEN
                      CASE @SortColumn                        
                          WHEN 'Id' THEN [MyTable].[Id]
                          WHEN 'TimeStamp' THEN [MyTable].[TimeStamp]
                          WHEN 'StatusCode' THEN [MyTable].[StatusCode]
  
                          ELSE [MyTable].[TimeStamp]
                      END
              END DESC,
              CASE @SortDirection
                  WHEN 'Asc' THEN
                      CASE @SortColumn                        
                          WHEN 'Id' THEN [MyTable].[Id]
                          WHEN 'TimeStamp' THEN [MyTable].[TimeStamp]                     
                          WHEN 'StatusCode' THEN [MyTable].[StatusCode]                       
  
                          ELSE [MyTable].[TimeStamp]
                      END             
              END ASC                 
          ) AS 'Row',
          [MyTable].[Id] AS 'Id',
          [MyTable].[TimeStamp] AS 'TimeStamp',
          [MyTable].[StatusCode] AS 'StatusCode'
      FROM  
          [MyTable]
      WHERE 
          (@Filter_Id is null or [MyTable].[Id] = @Filter_Id) AND     
          (@Filter_StatusCode is null or [MyTable].[StatusCode] = @Filter_StatusCode) AND
          (@Filter_FromTimeStamp IS NULL OR [MyTable].[TimeStamp] &gt;= @Filter_FromTimeStamp) AND
          (@Filter_TillTimeStamp IS NULL OR [MyTable].[TimeStamp] &lt;= @Filter_TillTimeStamp)
      ) AS t1
  WHERE 
      Row &gt;=@StartRecord AND 
      Row &lt;= @EndRecord
  </code></pre>