Re: Removing Last field from CSV string

From: Christian Ramseyer <rc(at)networkz(dot)ch>
To: pgsql-general(at)lists(dot)postgresql(dot)org, magnum11200(at)gmail(dot)com
Subject: Re: Removing Last field from CSV string
Date: 2020-05-16 19:59:24
Message-ID: 9e874908-71d4-b01b-fae4-b61a25453899@networkz.ch
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 16.05.20 17:18, Alex Magnum wrote:

> Now I try to remove the last  field and comma  ",Class"
>
> To get  Class V,Class VI,Class VII,Competitive Exam,Class VIII
>
> Is there a function or easy way to do this?
> Any help would be appreciated.
>

Hi Alex

Many options to do this with regexp_replace, here's one way:

with test as (
select 'Class VII,Competitive Exam,Class VIII,Class' as str
union
select 'Class VIIx,Competitive Exam22,Class VIIIabc,Classx'
)
select str, regexp_replace(str, '^(.*),(.*?)$', '\1') res from test;

|str
|res
|
|------------------------------------------------------|
|Class VII,Competitive Exam,Class VIII,Class
|Class VII,Competitive Exam,Class VIII

|------------------------------------------------------|
|Class VIIx,Competitive Exam22,Class VIIIabc,Classx
|Class VIIx,Competitive Exam22,Class
VIIIabc |

(I cut some columns at the start to better fit email width)

Cheers
Christian

--
Christian Ramseyer, netnea ag
Network Management. Security. OpenSource.
https://www.netnea.com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ron 2020-05-16 20:12:14 Re: Inherited an 18TB DB & need to backup
Previous Message Łukasz Dąbek 2020-05-16 17:56:20 Using b-tree index for >= condition when joining