在SQLite3中将字符串作为数字进行比较

时间:2022-10-25 05:32:40

I have the following query in SQLite:

我在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.

其中t1.field是包含数字的文本列。是否可以强制SQLite将t1.field的值视为数字而不是字符串(不做ALTER TABLE)?现在排序是一个纯字符串,所以10在2之前。

Thank you.

1 个解决方案

#1


17  

Well, found a solution:

好吧,找到了解决方案:

SELECT * FROM t1 ORDER BY t1.field + 0

The + 0 part seems to force conversion to number

+ 0部分似乎强制转换为数字

#1


17  

Well, found a solution:

好吧,找到了解决方案:

SELECT * FROM t1 ORDER BY t1.field + 0

The + 0 part seems to force conversion to number

+ 0部分似乎强制转换为数字