Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Ordering Table

Author  Topic 

musclebreast
Yak Posting Veteran

77 Posts

Posted - 2014-08-04 : 16:21:37
Hi,


I have one table with one column:

region

Global
USA
England
Germany
Spain



What I want is that the first entry Global is always in the first row the rest should be ordered by alphabet.

I tried this but order by works only for the result of union:


SELECT * FROM csappcidb_region WHERE region='Global'

UNION ALL

SELECT * FROM csappcidb_region WHERE region<>'Global' ORDER BY region


What else can I do?

Best regards,

Lara

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-08-04 : 16:26:33
select *
from csappcidb_region
order by case when region = 'Global' then 1 else case when region <> 'Global' then 2 end end

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2014-08-04 : 16:34:28
Alternativ:
select *
from csappcidb_region
order by case when region='Global' then 0 else 1 end
,region
Go to Top of Page

musclebreast
Yak Posting Veteran

77 Posts

Posted - 2014-08-04 : 16:39:29
Hi,

thanks. The second option works...i didn't know that I can use case in a order by statement. This gives me many new options.:)

Thnank you,

Lara
Go to Top of Page
   

- Advertisement -