Click here to Skip to main content
Licence CPOL
First Posted 11 Jan 2010
Views 14,622
Bookmarked 15 times

How to Sort Alphanumeric Data in SQL

By | 12 Jan 2010 | Article
Sort alphanumeric data in SQL.
 
Part of The SQL Zone sponsored by
See Also

Introduction

If your SQL query is not returning the result-set in the order you are expecting, this article may be helpful to fix the issue.

Background

We all know that the ORDER BY keyword is used to sort a result-set by a specified column. It works great for most of the cases. But, for alphanumeric data, it may not return the result-set that you will be expecting. This article explains how this can be fixed easily.

Using the Code

Step 1

I have created a table named “Test” with two columns, as shown below:

The following data has been added to the “Test” table:

image002.jpg

The “Order By” in the following SQL query may not return the result-set in the correct order.

Select ID From TestOrder by ID

image003.jpg

Step 2

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
Select ID 
From Test
ORDER BY
Case When IsNumeric(ID) = 1 then Right(Replicate('0',21) + ID, 20)
     When IsNumeric(ID) = 0 then Left(ID + Replicate('',21), 20)
     Else ID
End

image004.jpg

Step 3

I have changed the query to return the row numbers (used in pagination) and it worked!

(Note: ROW_NUMBER works only in SQL Server 2005 and above versions.)

Select Row_Number() Over (Order by
Case When IsNumeric(ID) = 1 then Right(Replicate('0',21) + ID, 20)
                          When IsNumeric(ID) = 0 then Left(ID + Replicate('',21), 20)
                        Else ID
               END) As RowNumber,
ID
From Test

image005.jpg

Any suggestions/comments are welcome!

Points of Interest

There may be better ways of doing this. Please share your thoughts.

History

  • 7th January, 2010: Initial version.

License

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

About the Author

Ravi Kallamadi



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 4 PinmemberSsafrin20:47 25 Oct '11  
QuestionHow to to do this while using LINQ PinmemberVishant Patil4:43 3 Aug '11  
Generalsorting alpha numeric data PinmemberSrabanjit guha3:10 31 Jan '10  
Generalthis works for me.. Pinmemberk_cire042615:04 19 Jan '10  
GeneralRe: this works for me.. PinmemberRavi Kallamadi10:53 20 Jan '10  
GeneralNot Really Working PinmemberAshfield2:45 18 Jan '10  
GeneralRe: Not Really Working PinmemberRavi Kallamadi16:35 18 Jan '10  
GeneralThanks. Works great PinmemberSubzeroDragon4:49 11 Jan '10  
GeneralRe: Thanks. Works great PinmemberRavi Kallamadi5:08 13 Jan '10  
GeneralRe: Thanks. Works great PinmemberSubzeroDragon4:08 14 Jan '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 12 Jan 2010
Article Copyright 2010 by Ravi Kallamadi
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid