Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Sorting alphanumeric data

$
0
0

I have data that is stored in a varchar(20) column that looks numeric but can be alphanumeric.

For instance:

 

I've tried this:

1a

11a

2a

3

5a

6

1

 

I want the order to be:

1

1a

2a

3

5a

6

11a

 

SQL I've tried is:

 

Code Snippet

SELECT * FROM (
    SELECT '1a' AS place
   UNION ALL SELECT '11a'    
UNION ALL SELECT '2a'
UNION ALL SELECT '3'
UNION ALL SELECT '5a'
UNION ALL SELECT '6'

UNION ALL SELECT '1'

) s ORDER BY CAST(LEFT(place, LEN(place)-1) AS INT)

 

 

but it hasn't solved my problem. Does anyone know how I can do this

in the ORDER BY?

 

The data in the column can be blank/Null otherwise it will always be a number follwed by a character.

 


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>