MySQL: How to order VARCHAR as INT

Sometimes it is needed to order type fields VARCHAR as INT. If you order a VARCHAR type field, the result can look like this:

1
10
2a
2b
2c
3a
3b
340
4
5

but, really, what you wanted is something like this:

1
2a
2b
2c
3a
3b
4
5
10
340

Follow this example:

ORDER BY
(core_gmoulds_obras_ctrl_dim_pecas.referencia+0) ASC,
core_gmoulds_obras_ctrl_dim_pecas.referencia ASC

Did it work for you? Please let me know in the comments below.

Exit mobile version