![]() |
Sort Order for varying length string.
Hi
I have searched for an answer but can't even find a starting point. I have a list of order number based on a prefixed date stamp: ORD201302161, ORD201302162, ORD201302163, ORD201302164, etc. The display is sorted using ASC, however they do not display as I would expect them to: ORD201302161 ORD2013021611 ORD2013021612 ORD2013021613 ORD2013021614 ORD2013021615 ORD2013021616 ORD2013021617 ORD2013021618 ORD2013021619 ORD201302162 ORD2013021620 How can I sort the order correctly?? Thanks Rob |
This is coz it is sorted as string, that is, if you would sort this numbers, as string, they would be sorted like this:
1 11 2 here is the hint: Code:
select 'ORD201302161', RIGHT('ORD201302161',LENGTH('ORD201302161')-3), RIGHT('ORD201302161',LENGTH('ORD201302161')-3) + 0 |
Easier than Bubikol's suggestion:
Code:
SELECT list, of, fields If the values might start with other 3-letter sequences, and if you want the values in order, including the letters, as for example: Code:
ABC331Code:
SELECT list, of, fields |
| All times are GMT +1. The time now is 09:39 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.