Return to Snippet

Revision: 32714
at October 1, 2010 22:53 by aamirrajpoot


Initial Code
Bin Way
===================================
SELECT 
tbl_column, 
BIN(tbl_column) AS binray_not_needed_column
FROM db_table
ORDER BY binray_not_needed_column ASC , tbl_column ASC

-----------------------

Cast Way
===================================
SELECT 
tbl_column, 
CAST(tbl_column as SIGNED) AS casted_column
FROM db_table
ORDER BY casted_column ASC , tbl_column ASC

Initial URL

                                

Initial Description
Okay,

If it is required to sort columns like this

1A
1a
10A
9B
21C
1C
1D

And Our desired Result is

1A
1C
1D
1a
9B
10A
21C

We can use casting, but casting will not sort 1A, 1B,1C properly. To overcome this issue we are  first converting this column to binary and sorting the binary combined with tbl_column.


hehe,,, after doing some headbanging just did it with casting too... and using same logic... damn why I didnt thought of that.

Initial Title
Alpha Numeric Sorting in MySQL

Initial Tags

                                

Initial Language
MySQL