I’ve got a Postgres ORDER BY issue with the following table:
em_code name
EM001 AAA
EM999 BBB
EM1000 CCC
To insert a new record to the table,
- I select the last record with
SELECT * FROM employees ORDER BY em_code DESC - Strip alphabets from em_code usiging reg exp and store in
ec_alpha - Cast the remating part to integer
ec_num - Increment by one
ec_num++ - Pad with sufficient zeors and prefix
ec_alphaagain
When em_code reaches EM1000, the above algorithm fails.
First step will return EM999 instead EM1000 and it will again generate EM1000 as new em_code, breaking the unique key constraint.
Any idea how to select EM1000?
The reason is that the string sorts alphabetically (instead of numerically like you would want it) and
1sorts before9.You could solve it like this:
It would be more efficient to drop the redundant ‘EM’ from your
em_code– if you can – and save an integer number to begin with.Answer to question in comment
To strip any and all non-digits from a string:
\Dis the regular expression class-shorthand for "non-digits".'g'as 4th parameter is the "globally" switch to apply the replacement to every occurrence in the string, not just the first.After replacing every non-digit with the empty string, only digits remain.