Numbers sort wrongly (1, 10, 2): VARCHAR column sorted as text
A numeric-looking column stored as text sorts alphabetically, so "10" comes before "2".
numbers sorted as text 1 10 2ORDER BY varchar number sorts incorrectlysorting numbers stored as VARCHAR'10' < '9' string comparison SQLnatural sort numbers SQL Meaning
Text comparison is character by character. Store numbers in numeric columns, or cast in the query (ORDER BY CAST(col AS INTEGER)), and prefer natural-sort expressions/zero padding only as a stopgap. Same issue affects comparisons: '10' < '9'.
Common causes
- Numeric data in VARCHAR/TEXT column
- CSV import without types
- Version strings sorted as text
- Mixed numeric and non-numeric values
- Collation affecting order
⚡ Quick fix
- Change the column type to INT/DECIMAL
ORDER BY CAST(col AS INT)as a workaround- Clean non-numeric values before converting
Detailed fix by platform
SQL
SELECT * FROM items ORDER BY CAST(sku_number AS INTEGER);
How to diagnose
- Column type — Text?
- Values — Numeric only?
- Cast — Safe?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
Most viewed in SQL
Other ways to find it
Where this error sits
Origin: Data. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 18 Nov 2026