SilverFish
SilverFish

Reputation: 1106

SQL Order By Case When

I have a SQL where I want to order by Name. But name has values 'NAME UNKNOWN' OR name of person (i.e. logged in user name). Is there a way to write ORDER BY in this case so that name of person comes first and then 'NAME UNKNOWN'?

Upvotes: 0

Views: 2852

Answers (2)

VDK
VDK

Reputation: 583

You could use something like this:

ORDER BY CASE WHEN name = 'NAME UNKNOWN' THEN 2 ELSE 1 END, name

Upvotes: 4

Stefano Zanini
Stefano Zanini

Reputation: 5926

Yes, you can write the order by clause like this

order by case when name = 'NAME UNKNOWN' then 'ZZZ' else name end

This way the 'NAME UNKNOWN' names will be counted as 'ZZZ'

Upvotes: 0

Related Questions