开发者

SQL and SQLite: how to search field starting with a number

开发者 https://www.devze.com 2023-02-25 20:48 出处:网络
I have a SQL model in which I want to search all words starting with a number. I can\'t use regexp since is not supported in SQLite, how can i trick it to have the same resu开发者_Python百科lts?you ca

I have a SQL model in which I want to search all words starting with a number. I can't use regexp since is not supported in SQLite, how can i trick it to have the same resu开发者_Python百科lts?


you can do this hack:

@results = YourModel.where("(your_field + 0) > 0")

It will return all objects from YourModel with your_field started with number that is more than zero (and not a zero)

Or little more native solution:

@results = YourModel.where("substr(your_field,1,1) IN (?)", (0..9).to_a)

It means that first character of your_field should be one of this numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9


You can use glob in sqlite.

Model.filter("field glob ?", '[0-9]*')
0

精彩评论

暂无评论...
验证码 换一张
取 消