Alex
Alex

Reputation: 179

remove characters from the strings matching pattern postgresql

I have some text elements in the column that have a character "|" and the rest right after it I need to get rid of:

campaign
abc
fdg|3234
dfr|4567

I want it to look like:

campaign
abc
fdg
dfr

I tried:

select replace (data_2_crm.campaign, '_|_', '' ) from data_2_crm
select trim (trailing '.*|' from campaign) from data_2_crm
select trim (trailing '%|' from campaign) from data_2_crm

It didn't work. Please help!

Upvotes: 0

Views: 3006

Answers (2)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520918

Use a regex replacement on the pattern \|.*$:

SELECT
    campaign,
    REGEXP_REPLACE(campaign, '\|.*$', '') AS new_campaign
FROM data_2_crm;

screen capture from demo link below

Demo

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269503

Use split_part():

select t.*, split_part(campaign, '|', 1)
from data_2_crm;

Upvotes: 1

Related Questions