SQL 仅当字符串以数值开头时才选择字符串的数字部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5651949/
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
Select only numeric part of string only if it starts with a numeric value
提问by Phill Pafford
This is only giving me the last character (numeric) but I need the whole numeric string
这只是给我最后一个字符(数字),但我需要整个数字字符串
SELECT substring('123 Main Street' FROM '%#"[0-9]#"%' FOR '#')
- Results: 3
- Expecting: 123
- 结果:3
- 预期:123
This gives me the same results but I need it to return a blank value:
这给了我相同的结果,但我需要它返回一个空白值:
SELECT substring('Main 123 Street' FROM '%#"[0-9]#"%' FOR '#')
- Results: 3
- Expecting:
- 结果:3
- 期待:
NOTE: Postgres 7.4
注意:Postgres 7.4
Helpful Link: http://www.postgresql.org/docs/7.4/static/functions-matching.html
有用的链接:http: //www.postgresql.org/docs/7.4/static/functions-matching.html
UPDATE:
更新:
SELECT substring('Main 123 Street' FROM '[0-9]+')
SELECT substring('123 Main Street' FROM '[0-9]+')
- Both now return: 123
- Still need to skip or return full string of: 'Main 123 Street'
- 现在两者都返回:123
- 仍然需要跳过或返回完整字符串:'Main 123 Street'
UPDATE 2:
更新 2:
Almost have it:
差不多了:
This gives me the results I want if it doesn't start with a numeric value:
如果它不以数值开头,这会给我我想要的结果:
SELECT
COALESCE(substring('Main 123 Street' FROM '[0-9]*') || 'Main 123 Street', ''),
substring('Main 123 Street' FROM '[0-9]*')
But this gives me both and I only want the second condition:
但这给了我两个,我只想要第二个条件:
SELECT
COALESCE(substring('123 Main Street' FROM '[0-9]*') || '123 Main Street', ''),
substring('123 Main Street' FROM '[0-9]*')
I GOT IT!!! Thanks for all who posted:
我知道了!!!感谢所有发帖的人:
SELECT CASE
WHEN COALESCE(substring(db_column FROM '[0-9]*'), db_column) != '' THEN COALESCE(substring(db_column FROM '[0-9]*'), db_column)
ELSE db_column
END AS addsress_string
FROM db_table
采纳答案by Justin Morgan
I don't know postgresql regex syntax, but in most regex you would write [0-9]+
. Without the quantifier, [0-9]
matches a single character.
我不知道 postgresql 正则表达式的语法,但在大多数正则表达式中你会写[0-9]+
. 没有量词,[0-9]
匹配单个字符。
回答by rmmh
I don't have a Postgres install to test with, but something like this might work:
我没有要测试的 Postgres 安装,但这样的事情可能会奏效:
SELECT substring('123 Main Street' FROM '^[0-9]+')
SELECT substring('123 Main Street' FROM '^[0-9]+')
That returns nothing if it doesn't start with a number. If you want to return the full string instead, this should work:
如果它不以数字开头,则不返回任何内容。如果你想返回完整的字符串,这应该有效:
SELECT substring('123 Main Street' FROM '^[0-9]+|.*')
SELECT substring('123 Main Street' FROM '^[0-9]+|.*')
回答by Allan Registos
Maybe it is not yet too late to improve on rmmh's answer.
也许现在改进 rmmh 的答案还为时不晚。
This works for me:
这对我有用:
SELECT substring(column_name FROM '[0-9]+') FROM TableName;
In postgresql, just remove the caret for the numbers to show.
在 postgresql 中,只需删除要显示的数字的插入符号。
if column_name = 'XXXX-0001'
the result will be:
如果column_name = 'XXXX-0001'
结果是:
0001