postgresql 如何在PostgreSQL中选择以数字开头的行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2662088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-20 00:06:18  来源:igfitidea点击:

How to select rows which start with digit in PostgreSQL?

sqlpostgresql

提问by noxvile

Need to get rows starting with digit e.g. '1test', '32 test'. I tried

需要获取以数字开头的行,例如“1test”、“32 test”。我试过

SELECT * FROM table WHERE name LIKE '[0-9]%'

as I used to do in MSSQL but it wasn't successful.

就像我以前在 MSSQL 中所做的那样,但它没有成功。

回答by Mark Byers

Try this:

尝试这个:

SELECT * FROM table WHERE name ~ '^[0-9]'

This uses a POSIX regular expression.

这使用 POSIX 正则表达式。

回答by Max Shawabkeh

According to the docs, you can use SIMILAR TOinstead of LIKEto do regex-like matching, and ~to do full POSIX regex matching.

根据docs,您可以使用SIMILAR TO而不是进行LIKE类似正则表达式的匹配,并~进行完整的 POSIX 正则表达式匹配。

回答by Laurenz Albe

This solution is probably faster than a regular expression, since regular expressions are CPU intensive:

此解决方案可能比正则表达式更快,因为正则表达式是 CPU 密集型的:

SELECT ... FROM "table" WHERE substr(name, 1, 1) BETWEEN '0' AND '9';