I have a varchar column encoding code, this code can only contain numbers or a number prefixed with char, for example, I have a column containing this data:
+------+ | Code | +------+ | 1 | | C1 | | 2 | | 3 | | C3 | | F3 | | F1 | | F17 | | C9 | | C10 | | C47 | | C100 | | C134 | | A234 | |C1245 | | 10 | | 100 | +------+
Etc...
I want to sort this column according to the following rules:
- Numeric Code Only
- The prefix code with alphabetic ordering is alfanumerically and the numerical part ordered as a number
I want to get a result set ordered as follows:
+------+ | Code | +------+ | 1 | | 2 | | 3 | | 10 | | 100 | | A234 | | C1 | | C3 | | C9 | | C10 | | C47 | | C100 | | C134 | |C1245 | | F1 | | F3 | | F17 | +------+
How can I get a result set ordered by these criteria? I tried with this request:
SELECT Code FROM Code_List ORDER BY case when Code like '%[az]%' then 99999999999999999999999999999999 else convert(decimal, Code) end
But I get a result that first orders the number, and then the prefix number, but the alpha prefix number is ordered as char, and not the way I want it ...
A single numerical entry must be ordered in accordance with the rules of numerical order and character order, therefore, if a single numerical entry:
+------+ | Code | +------+ | 1 | | 47 | | 2 | | 3 | | 6 | | 100 | | 112 | | 10 |
I want to receive:
+------+ | Code | +------+ | 1 | | 2 | | 3 | | 6 | | 10 | | 47 | | 100 | | 112 |
Database is Microsoft SQL Server.
source share