Re: Removing Last field from CSV string - Mailing list pgsql-general

From Christian Ramseyer
Subject Re: Removing Last field from CSV string
Date
Msg-id 9e874908-71d4-b01b-fae4-b61a25453899@networkz.ch
Whole thread Raw
In response to Removing Last field from CSV string  (Alex Magnum <magnum11200@gmail.com>)
List 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








pgsql-general by date:

Previous
From: Łukasz Dąbek
Date:
Subject: Using b-tree index for >= condition when joining
Next
From: Ron
Date:
Subject: Re: Inherited an 18TB DB & need to backup