How to sort AlphaNumeric data in SQL Server

Kirthiga
Posted by Kirthiga under Sql Server category on | Points: 40 | Views : 3148
Let us consider a table with Alphanumeric values.

Declare @AlphaNum table(Value varchar(10))
insert into @AlphaNum values ('AP1'),('AP2'),('AP13'),('AP30'),('Ker3'),('Ker30'),('Ker4')
,('WstBg1'),('WstBg11'),('WstBg111'),('WstBg23'),('WstBg203')
select * from @AlphaNum


To sort AlphaNumeric data

select Value from @AlphaNum
order by substring(Value,1,patindex('%[0-9]%',Value)-1)
,cast(STuff(Value,1,patindex('%[0-9]%',Value)-1,'000') as int)

Comments or Responses

Login to post response