所以基本上我需要获得邮政编码字符串的一部分,所有这些都在空白空间之前
我目前的查询是
select postcode, left(postcode, length(postcode) - strpos(' ', postcode))
from postcodetable
但它只是没有正确返回邮政编码,例如:第一列是NW1 1AA
,第二列应该只是NW1
而是它只是重复第一列
你对strpos()
的论点是错误的。所以你可以这样做:
select postcode, left(postcode, length(postcode) - strpos(postcode, ' '))
from (values ('NW1 1AA')) v(postcode);
您也可以使用带有正则表达式的substring()
执行此操作:
select postcode, substring(postcode from '[^ ]+'::text)