Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare strings as numbers in SQLite3

I have the following query in SQLite:

SELECT * FROM t1 ORDER BY t1.field

Where t1.field is a text column containing numbers. Is it posible to force SQLite to consider the values of t1.field as numbers instead of strings (whithout doing ALTER TABLE)? Right now the sort is a pure string one, so 10 goes before 2.

Thank you.

like image 975
diegogs Avatar asked Nov 17 '10 12:11

diegogs


1 Answers

Well, found a solution:

SELECT * FROM t1 ORDER BY t1.field + 0

The + 0 part seems to force conversion to number

like image 163
diegogs Avatar answered Oct 11 '22 13:10

diegogs