Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have to sort a column contains value like

AZC1
AZC10
AZC12
AZC100
AZC2
AZC24
AZC3
AZC34

My required output is

VB
AZC1
AZC2
AZC3
AZC10
AZC12
AZC24
AZC34
AZC100


(ie) It should sort Alpha values first and then numeris values. Numeric values should be sorted like 1,2,3,10,24....
Posted
Comments
Thava Rajan 11-Jun-14 7:39am    
is first three characters are always alphabets?
if it contains data like this
az1
az2
azc1
azc2
then it is almost a complex query
other wise it is a simple query

 
Share this answer
 
Comments
Nandakishore G N 11-Jun-14 8:43am    
Good suggestion..My 5.
Thava Rajan 12-Jun-14 3:37am    
this will not order the alphabets it only order the numeric parts
ok i gave a simple solution
SQL
select * from test
order by substring(Zone,1,3) ,
cast(STuff(Zone,1,3,'000')  as int)

ok here is a complex one

SQL
select * from test
order by substring(Zone,1,patindex('%[0-9]%',zone)-1) ,
cast(STuff(Zone,1,patindex('%[0-9]%',zone)-1,'000')  as int)

if you want to check it
use this Sql Fiddle[^]
 
Share this answer
 
v2
Comments
Magic Wonder 11-Jun-14 8:50am    
Yeah it is simple and sweet but it is limited. What is data contains ABHCDE12 or ABCD1?
Thava Rajan 12-Jun-14 3:36am    
well solution updated
Magic Wonder 12-Jun-14 3:40am    
Yeah Thats Good.
kirthiga S 12-Jun-14 1:22am    
@Thava Rajan

Thank u. Your query is simple and working for my scenario. We can use this for limited case.
Thava Rajan 12-Jun-14 3:36am    
solution updated please check it out

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900