简体   繁体   中英

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

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 results?

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

您可以在sqlite中使用glob

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM