Sort Alpha Numeric data in SQL Server

Sorting a alphanumeric data, A sample data is as below

image002.jpg

When i sort the data it looks like below
image003.jpg

I have modified the ORDER BY clause as shown below, and it returned the results in the proper order.

(Note: The ID column is defined as varchar(20). So, I did the following to fix this issue:

If ID is numeric, add 21 '0's in front of the ID value and get the last 20 characters

If ID is not numeric, add 21 ‘’s at the end of the ID value and get the first 20 characters


image004.jpg

Comments

Post a Comment

Your comment or feedback help me to write more blogs. Please add your comments.

Popular posts from this blog

How to Stop Excel from giving a number in scientific notation

The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machine

System.IO.IsolatedStorage.IsolatedStorageException: Unable to create the store directory