Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

order by problem

Status
Not open for further replies.

fikir

Programmer
Jun 25, 2007
86
0
0
I have this table and trying to order by

Code:
declare @tbl table (col varchar(100))

insert into @tbl
select '((ABC)+F)'
union all select '123'
union all select 'ABC+F'
union all select 'DEF'
union all select 'XYZ'

I wrote this query 

select *
from @tbl
order by case when left(col, 2) = '((' then 'ABC+F1' end

to sort the data 

I wanted it to be 

col

123
ABC+F
((ABC)+F)
DEF
XYZ

but it is giving me the following resultset

col

123
ABC+F
DEF
XYZ
((ABC)+F)

[\code]

how can I change my code to get the right order

Thanks,
 
Code:
declare @tbl table (col varchar(100))

insert into @tbl
select '((ABC)+F)'
union all select '123'
union all select 'ABC+F'
union all select 'DEF'
union all select 'XYZ'

I wrote this query 

select *
from @tbl
order by case when left(col, 2) = '((' then 'ABC+F1' end

to sort the data 

I wanted it to be 

col

123
ABC+F
((ABC)+F)
DEF
XYZ

but it is giving me the following resultset

col

123
ABC+F
DEF
XYZ
((ABC)+F)
 
Fill in the "else" part of the case statement:

Code:
select *
from @tbl
order by case 
	when left(col, 2) = '((' then 'ABC+F1' 
	else col
end

returns the desired results:

123
ABC+F
((ABC)+F)
DEF
XYZ
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top