Trim PostgreSQL 中的英国邮政编码
Trim UK Postcodes in PostgreSQL
我知道一个类似的问题,但该解决方案在 PostgreSQL 中不起作用。
我想做什么;使用完整邮政编码的副本创建新列,然后 trim 首先到部门,然后 trim 到地区,最后到地区。 IE。将邮政编码复制到 postcode_sector trim postcode_sector.
TA15 1PL
变为:
TA15 1
扇区
TA15
为区
TA
面积。
我尝试过的:
在 table 中为每个创建新列,然后;
SELECT postcode_sector FROM postcodes
RTRIM (Left([postcode_sector],(Len([postcode_sector])-2)) + " " +
Right([postcode_sector],3));
抛出语法错误;
Select
Postcode,
RTRIM(LEFT(Postcode, PATINDEX('%[0-9]%', Postcode) - 1)) As AreaTest
From postcodes
在 PostgresSQL 中不能像没有 PATINDEX 函数那样工作。从这里开始,我研究了另一种使用 SUBSTRING
函数的替代方法,该方法由优秀教程 here 提供。使用;
SELECT
substring (postcode FROM 1 FOR 6) AS postcode_sector
FROM postcodes;
让我分道扬镳,我现在有一个 TA15 1 的专栏,但由于系统的工作方式,我也有 T15 1A。 PostgresSQL 中有没有一种方法可以计算单元格中的字符数并删除一个?出于更广泛的兴趣,使用 TRIM
比 SUBSTRING
更快 我正在执行整个邮政编码文件,该文件大约有 2700 万行
这似乎可以做到:
with postcodes (postcode) as (
values ('TA15 1PL')
)
select substring(postcode from '[^0-9]{2}[0-9]+ [0-9]') as sector,
substring(postcode from '[^0-9]{2}[0-9]+') as district,
substring(postcode from '([^0-9]+)') as area
from postcodes;
returns
sector | district | area
-------+----------+-----
TA15 1 | TA15 | TA
我不太熟悉英国 post 代码,但根据 Wikipedia's format,这应该可以处理所有情况:
select postcode,
m[1] || m[2] || ' ' || m[3] sector,
m[1] || m[2] district,
m[1] area
from src,
regexp_matches(postcode, '^([A-Z]{1,2})([0-9A-Z]{1,2}) ([0-9])([A-Z]{2})') m
我知道一个类似的问题
我想做什么;使用完整邮政编码的副本创建新列,然后 trim 首先到部门,然后 trim 到地区,最后到地区。 IE。将邮政编码复制到 postcode_sector trim postcode_sector.
TA15 1PL
变为:
TA15 1
扇区TA15
为区TA
面积。
我尝试过的:
在 table 中为每个创建新列,然后;
SELECT postcode_sector FROM postcodes
RTRIM (Left([postcode_sector],(Len([postcode_sector])-2)) + " " +
Right([postcode_sector],3));
抛出语法错误;
Select
Postcode,
RTRIM(LEFT(Postcode, PATINDEX('%[0-9]%', Postcode) - 1)) As AreaTest
From postcodes
在 PostgresSQL 中不能像没有 PATINDEX 函数那样工作。从这里开始,我研究了另一种使用 SUBSTRING
函数的替代方法,该方法由优秀教程 here 提供。使用;
SELECT
substring (postcode FROM 1 FOR 6) AS postcode_sector
FROM postcodes;
让我分道扬镳,我现在有一个 TA15 1 的专栏,但由于系统的工作方式,我也有 T15 1A。 PostgresSQL 中有没有一种方法可以计算单元格中的字符数并删除一个?出于更广泛的兴趣,使用 TRIM
比 SUBSTRING
更快 我正在执行整个邮政编码文件,该文件大约有 2700 万行
这似乎可以做到:
with postcodes (postcode) as (
values ('TA15 1PL')
)
select substring(postcode from '[^0-9]{2}[0-9]+ [0-9]') as sector,
substring(postcode from '[^0-9]{2}[0-9]+') as district,
substring(postcode from '([^0-9]+)') as area
from postcodes;
returns
sector | district | area
-------+----------+-----
TA15 1 | TA15 | TA
我不太熟悉英国 post 代码,但根据 Wikipedia's format,这应该可以处理所有情况:
select postcode,
m[1] || m[2] || ' ' || m[3] sector,
m[1] || m[2] district,
m[1] area
from src,
regexp_matches(postcode, '^([A-Z]{1,2})([0-9A-Z]{1,2}) ([0-9])([A-Z]{2})') m