SQLite Sorts 100 Before 20: Check the Stored Type Before Editing Values
Check typeof(amount) on the stored rows. If the values are text, ORDER BY amount can sort them lexically, placing 100 before 20. The column's declared affinity informs SQLite conversions, and the stored value still has its own type.
Count storage classes in the affected data
SELECT typeof(amount) AS storage_type, COUNT(*)
FROM invoices
GROUP BY typeof(amount);Inspect examples from each group and discover whether values contain spaces, units or placeholders. A column named amount conveys business meaning but does not prove a uniform storage class. If there are numbers and text in one column, ordering also depends on the different classes.
Use casts on validated values
In a synthetic fixture of text values 100, 20 and 5 governed by an integer-only input contract, ORDER BY CAST(amount AS INTEGER) produces 5, 20 and 100. Validate the contract before relying on a cast, because SQLite can coerce malformed input in ways that hide a data error. Avoid changing a table until values outside the contract are accounted for.
Test ties, missing values and future inserts
Compare sorted results on valid integers, NULLs and an invalid string. Add a stable secondary order such as a unique ID when the same amount can recur. If a permanent schema adjustment is needed, rehearse it on a verified copy and confirm that subsequent loads also enforce the expected data type.
댓글
댓글 쓰기
안녕하세요 :)