如何在postgres中的空格之前获取所有字符串?

问题描述 投票:1回答:1

所以基本上我需要获得邮政编码字符串的一部分,所有这些都在空白空间之前

我目前的查询是

select postcode, left(postcode, length(postcode) - strpos(' ', postcode)) 
from postcodetable

但它只是没有正确返回邮政编码,例如:第一列是NW1 1AA,第二列应该只是NW1而是它只是重复第一列

sql string postgresql
1个回答
3
投票

你对strpos()的论点是错误的。所以你可以这样做:

select postcode, left(postcode, length(postcode) - strpos(postcode, ' '))
from (values ('NW1 1AA')) v(postcode);

您也可以使用带有正则表达式的substring()执行此操作:

select postcode, substring(postcode from '[^ ]+'::text)
© www.soinside.com 2019 - 2024. All rights reserved.