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
 Order by

Author  Topic 

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2012-03-13 : 02:29:37
I have a query to order by ascending to apply in checkbox.
Can I order by start with Hello?

Name
Hello
Amy
Brian
Caty
.
.
.
.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-03-13 : 02:35:45
[code]order by case when Name = 'Hello' then 1 else 2 end, Name[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2012-03-13 : 02:49:00
What does 1 and 2 meant?
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-03-13 : 02:53:35
just numbers. when name is HELLO, the 1st ORDER BY parameter will have 1 and hence will be sorted first before the rest (others will be 2)


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-03-13 : 02:55:09
if you still don't get that, see this

select NAME, sort = case when Name = 'Hello' then 1 else 2 end
from yourtable
ORDER BY sort, NAME



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2012-03-13 : 03:13:21
select 'ALL' as Name, NULL as label
union
select (label + ' (' + Name + ')') as label, Name
from tablename
order by case when Name = 'All' then 1 else 2 end, Name

im getting error invalid column name 'Name'
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-03-13 : 03:44:26
[code]
select Name, label
from
(
select 'ALL' as Name, NULL as label

union

select (label + ' (' + Name + ')') as label, Name
from tablename
) a
order by case when Name = 'All' then 1 else 2 end, Name
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -