提取字段的第一个数字部分 [英] Extract first numeric part of field
本文介绍了提取字段的第一个数字部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个地址的数据库(Postgres 7.4)字段
I have a database (Postgres 7.4) field for address
示例数据
address | zip
-----------------------+-------------+
123 main street | 12345
-----------------------+-------------+
3 where road | 12345
-----------------------+-------------+
South 3 where road | 12345
查询
SELECT *
FROM tbl
WHERE zip = 12345
AND address ILIKE '3%'
我得到全部,但我不想要123条主要街道
I get all but I don't want 123 main street
SELECT *
FROM tbl
WHERE zip = 12345
AND address ILIKE '123%'
我得到了想要的结果
我的问题是我该如何匹配地址的数字部分?
My question is how do I just match the numeric part of the address?
推荐答案
尝试一下:
SELECT substring(address, '^\\d+') AS heading_number
FROM tbl
WHERE zip = 12345
AND address ILIKE '3%'
从字符串开头返回1个或多个数字。
省略锚点 ^
如果要在字符串中使用第一个数字序列,而不是在开头的序列。示例:
Returns 1 or more digits from the start of the string.
Leave out the anchor ^
if you want the first sequence of digits in the string instead of the sequence at the start. Example:
SELECT substring('South 13rd street 3452435 foo', '\\d+');
了解有关 substring()和正则表达式在手册中。
在最新版本(8.0+)中,请不要忘记将其用于转义字符串语法:
Read about substring() and regular expressions in the manual.
In more recent versions (8.0+), don't forget to use for escape string syntax like this:
SELECT substring('South 13rd street 3452435 foo', E'\\d+');
这篇关于提取字段的第一个数字部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文