Hi,
Been doing quite a fair amount of bespoke development recently, thought I’d share this as it’s bound to help some one out in the future.
If you ever find your self needing to sort (ORDER BY) a custom field by a numeric value you may find yourself becoming unstuck.
The “problem” is that EE stores all custom field data as VARCHAR. This causes an issue when trying to sort on a numeric value.
e.g.
Imagine you have the following record set of ids:
200
100
10000
2000
1000
20000The following SQL would sort them like so:
mysql> SELECT column FROM table_name ORDER BY column;
column
======
100
1000
10000
200
2000
20000
...Which, whilst ordering correctly is undesirable. The following quick fix allows you to sort numerically on varchar fields without having to use CAST.
mysql> SELECT column FROM table_name ORDER BY column+0;
column
======
100
200
1000
2000
10000
20000
...Hope someone finds this as useful as I did.