Click here to Skip to main content
15,895,841 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How to order the varchar fiels as like number where first 2 char are fixed

eg I Have values like

mr1
mr100
mr2
mr110
mr101
mr1000

and the order valus should be like

mr1
mr2
mr100
mr101
mr110
mr1000
Posted
Updated 22-Jul-14 23:57pm
v3

As Carlo said, you need to extract numeric part in ORDER clause.

SQL
SELECT ColumnName
FROM TableName
ORDER BY CONVERT(INT, RIGHT(ColumnName, LEN(ColumnName)-2))
 
Share this answer
 
Comments
ZurdoDev 23-Jul-14 7:19am    
+5
Maciej Los 23-Jul-14 7:19am    
Thank you ;)
Just extract the numeric part of the field and base your order clause on that.
 
Share this answer
 
Comments
[no name] 23-Jul-14 6:08am    
how to extract and order the field ? Can you give with example
Maciej Los 23-Jul-14 6:32am    
+5!
CPallini 23-Jul-14 7:15am    
Thank you man and have my 5 as well.
SELECT [COLUMN Name] FROM [Table Name] ORDER BY CONVERT(INT,RIGHT(COLUMN Name,LEN(COLUMN Name)-3))

Try this..it will work..
 
Share this answer
 
Comments
ZurdoDev 23-Jul-14 7:24am    
This solution was already posted.

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