如何删除、替换 postgresql 中某个字段的数据以简化它们?

How can I Delete, replace the data of a field in postgresql to simplify them?

我被困在这一点上,我正在尝试使用 postgresql 清理 table 的行名称。 table 适用于威尔士和英格兰的地区。

我正在尝试删除字符串 'District'、'(B)'、'London Boro' 以及双重英语 - 威尔士名称中连字符之前的所有内容,例如 (Swansea - Abertawe ) 我想消除连字符之前的所有内容(无论如何我想知道如何在两个方向上都这样做)并得到(Abertawe)。我使用前 3 个字符串进行管理,但使用连字符部分我找不到问题的解决方案。

我试过使用替换和 trim 但我没有得到想要的结果。

这是我上次试过的代码:

select name,
trim(replace(replace(replace(replace(name, 'District', ''), '(B)', ''), 'London Boro', ''),'% - %', ''), ' - ') 
from district;

这是 table 使用的示例

    name
    Swansea - Abertawe
    Barnsley District (B)
    Bath and North East Somerset
    Bedford (B)
    Birmingham District (B)
    Blackburn with Darwen (B)
    Blackpool (B)
    Blaenau Gwent - Blaenau Gwent
    Bolton District (B)
    Bournemouth (B)
    Bracknell Forest (B)
    Bradford District (B)
    The Vale of Glamorgan - Bro Morgannwg
    Aylesbury Vale District
    Chiltern District
    South Bucks District
    Wycombe District
    Bury District (B)
    Cardiff - Caerdydd
    Caerphilly - Caerffili
    Calderdale District (B)
    Cambridge District (B)
    East Cambridgeshire District
    City of Westminster London Boro
    Croydon London Boro
    Ealing London Boro
    Enfield London Boro
Castell-nedd Port Talbot - Neath Port Talbot

这就是我想要得到的:

 name
    Abertawe
    Barnsley
    Bath and North East Somerset
    Bedford
    Birmingham
    Blackburn with Darwen
    Blackpool
    Blaenau Gwent
    Bolton
    Bournemouth
    Bracknell Forest
    Bradford
    Bro Morgannwg
    Aylesbury Vale
    Chiltern
    South Bucks
    Wycombe
    Bury
    Caerdydd
    Caerffili
    Calderdale
    Cambridge
    East Cambridgeshire
    City of Westminster
    Croydon
    Ealing
    Enfield
Neath Port Talbot

谢谢,

用Case语句区分两个条件,用Position函数判断-是否存在

试试这个

select
name,
case when position(' - ' in name)>0 then 
trim(replace(replace(replace(substr(name,position(' - ' in name)+3,length(name)), 'District', ''), '(B)', ''), 'London Boro', ''))
 else 
 trim(replace(replace(replace(name, 'District', ''), '(B)', ''), 'London Boro', ''))
 end 
from district

DEMO