neethu neethu - 4 days ago 5
MySQL Question

How to display names starting with 's' and then names having 's'

I need to select names starting with 's' and names having 's'.for that I used the below command:

select staffname from staffreg where staffname like 's%' or staffname like '%s%'


But I need to display the names starting with 's' first and then names having 's' only.

for example table may contains:

Sushama
Asha
Suresh

I need to display 'Asha' after Sushama and Suresh:

Answer
select staffname from staffreg where staffname like 's%' or staffname like '%s%' order by
    case
        when lower(staffname) like 's%' THEN 1
        else 2
    end;
Comments