Home >Database >Mysql Tutorial >How Can I Sort Varchar Columns Containing Numeric Values in MySQL?
Numeric Sorting of Varchar Columns in MySQL
When dealing with varchar columns that contain numeric values, sorting them as numbers can be a challenge. One approach to address this is by using the expression "* 1".
Using "* 1" for Numeric Sorting
Consider a table with a varchar column called "number_as_char" containing values such as "17.95", "199.95", and "139.95". To sort this column numerically, you can use the following query:
<code class="mysql">SELECT * FROM tbl ORDER BY number_as_char * 1;</code>
The "* 1" expression effectively converts the varchar values to numeric values. It allows you to sort the column as numbers, regardless of leading zeros or other non-numeric characters.
Benefits of Using "* 1"
Using the "* 1" expression offers several advantages:
The above is the detailed content of How Can I Sort Varchar Columns Containing Numeric Values in MySQL?. For more information, please follow other related articles on the PHP Chinese website!