regex in SQL to detect one or more digit

别等时光非礼了梦想. 提交于 2019-12-01 19:26:06

Use REGEXP operator instead of LIKE operator

Try this:

SELECT '129387 store' REGEXP '^[0-9]* store$';

SELECT * FROM shop WHERE `name` REGEXP '^[0-9]+ store$';

Check the SQL FIDDLE DEMO

OUTPUT

|         NAME |
|--------------|
| 129387 store |
Barmar

If you mean MySQL, LIKE does not implement regular expressions. It implements the much more restricted SQL pattern matching, which just has two special operators: % matches any sequence of characters, and _ matches any single character.

If you want regular expression matching, you must use the REGEXP or RLIKE operator:

SELECT *
FROM shop
WHERE name REGEXP '[0-9]+ store'

MySQL's regular expression language doesn't include \d to specify digits, but you could write that as:

SELECT *
FROM shop
WHERE name REGEXP '[[:digit:]]+ store'

If the store name must begin with digits, you need an anchor:

SELECT *
FROM shop
WHERE name REGEXP '^[0-9]+ store'

You can learn more about regular expression syntax at regular-expressions.info.

Try

SELECT * 
FROM  `shop` 
WHERE  `name` LIKE  '%[0-9] store%'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!