How can I sort in an SQL statement, while giving numbers a lower value than characters?
e.g.:
- ‘iPad Wi-Fi’ should appear before ‘iPad 2 Wi-Fi’
- ‘iPad 2 Wi-Fi’ should appear before ‘iPad 3 Wi-Fi’
- ‘iPad 3 Wi-Fi’ should appear before ‘iPhone’
- ‘iPhone’ should appear before ‘iPhone 2’
Probably the easiest way is to have a separate column which you populate in a way that it sorts as you want it.
Then make sure that it remains consistent by ensuring it’s set by an insert/update trigger.
By using a separate column set on insert/update, you guarantee the cost of calculating this order is done only where needed, not every time you
select. It’s often the case that DBAs will sacrifice space to get more time.And the use of triggers maintains the ACID properties.