Case Statement in Select Not Working
<p>This is for SQL Server 2012 database... </p>

<p>I'm reading a varchar data column from a table, and depending on user-selected options the data could be either alphanumeric or numeric.  I need to sort by this column so I'm trying to use a case statement, but it doesn't seem to be working.  Below is a simplified example of what I'm doing, but as you can see, it's falling through to the Else of the case statement in both scenarios...any ideas on what I'm doing wrong?</p>

<pre><code>Select  '1st Grade Math' topic Into #temp

Declare @rptView int
Set @rptView = 1

Select  Case @rptView 
      When 1 Then topic 
      Else cast(topic as int)
    End 
From    #temp 
Order by Case @rptView 
      When 1 Then topic 
      Else cast(topic as int)
     End 

Select  Case 
      When @rptView = 1 Then topic 
      Else cast(topic as int)
    End 
From    #temp 
Order by Case @rptView 
      When 1 Then topic 
      Else cast(topic as int)
     End 

drop table #temp 
</code></pre>