Yogesh Jindal Yogesh Jindal - 6 months ago 22
SQL Question

orderby in sql query

I need to order sql query by a column (the three different values in this column are C,E,T).

I want the results in order of E,C,T. So, of course I can't use ascending or descending orderby on this column.

Any suggestions how can I do this? I don't know if that matters or not but I am using sybase data server on tomcat.

Answer

The other methods work, but this is an often overlooked trick (in MSSQL, I'm not positive if it works in Sybase or not):

select
  foo,
  bar
from
  bortz
order by
  case foo 
    when 'E' then 1
    when 'C' then 2 
    when 'T' then 3 
    else 4 
  end