0

Let's say I have this in a column:

'a'
'b'
'c'
'd'
'e'

I want the data to be sorted and looked like this:

'a'
'b'
'd'
'e'
'c'

Is this possible?

2 Answers2

2
select letter
from letters
order by
   case letter
     when 'a' then 0
     when 'b' then 1
     when 'd' then 2
     when 'e' then 3
     when 'c' then 4
   end
Xedni
  • 3,662
  • 2
  • 16
  • 27
  • 1
    This is just the ans from https://stackoverflow.com/questions/19196475/custom-order-by-in-sql-server-for-eg-p-a-l-h-not-asc-or-desc. – MT-FreeHK Sep 14 '18 at 03:05
2

An other way around using CASE statement is,

Query

select [column_name] 
from [your_table_name]
order by 
  case [column_name] 
  when 'c' then 2 
  else 1 end, 
[column_name];

Find a demo here

Ullas
  • 11,450
  • 4
  • 33
  • 50