I have short postcodes in a column ‘postcode’. By short I mean SW1, BN2, BN22 etc, just the first part.
I need to list them in this order for eg.
BN2
BN22
CR1
CR12
SW1
SW12
The sql I have worked out so far is:
ORDER BY length(postcode), postcode
which works for the same first 2 chars, like:
BN2
BN22
but the whole list is not in order with sw coming before bn etc. I know there must be a way… Does anyone have any ideas please, or pointers to a tutorial. I have searched the Mysql site but can’t find a way, or can’t get my head round the general instructions to make a way.
That’s why I tried the: ORDER BY length(postcode), postcode
which works if they were all KT but goes out when you introduce other prefixes.
Easy enough to add 2 more cols for this situation though so I have taken this route for the moment. If there is another way, I am always looking to learn
You should be able to write a user-defined sort in PHP, using usort() and strnatcmp() - the data should be in an array. That nat in the second function is for natural sort, which is what you are after.
Or so I read this morning when I decided to do some revision of PHP…
It doesn’t look difficult - not that I’ve tried it before.