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
How to select rows which start with digit in PostgreSQL?
提问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
回答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';