Removing Last field from CSV string

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Removing Last field from CSV string

Alex Magnum
Hi,

I have a string that I want to cut to 60 char and then remove the last field and comma.

substring('Class V,Class VI,Class VII,Competitive Exam,Class VIII,Class X,Class XI,Class IX,Class XII',1,60);

substring | Class V,Class VI,Class VII,Competitive Exam,Class VIII,Class

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.

Thank you
Alex
Reply | Threaded
Open this post in threaded view
|

Re: Removing Last field from CSV string

PALAYRET Jacques
Hello,

Perhaps, a statement like :
   substring(theString, 1, length(theString)-position(',' IN reverse(theString)))

with theString   'Class V,Class VI,Class VII,Competitive Exam,Class VIII*,Class'  for example.
Regards
----- Météo-France -----
PALAYRET JACQUES
DCSC/MBD
[hidden email]
Fixe : +33 561078319
Reply | Threaded
Open this post in threaded view
|

Re: Removing Last field from CSV string

Adrian Klaver-4
On 5/16/20 9:31 AM, PALAYRET Jacques wrote:
> Hello,
>
> Perhaps, a statement like :
>     substring(theString, 1, length(theString)-position(',' IN
> reverse(theString)))
>
> with theString   'Class V,Class VI,Class VII,Competitive Exam,Class
> VIII*,Class' for example.

That's cool. I did a little fiddling with above:

SELECT
     substring(
     left('Class V,Class VI,Class VII,Competitive Exam,Class VIII,Class
X,Class XI,Class IX,Class XII', 60), 1, length(
     left('Class V,Class VI,Class VII,Competitive Exam,Class VIII,Class
X,Class XI,Class IX,Class XII', 60)) - position(',' IN reverse(
     left('Class V,Class VI,Class VII,Competitive Exam,Class VIII,Class
X,Class XI,Class IX,Class XII', 60))))


   substring
--------------------------------------------------------
  Class V,Class VI,Class VII,Competitive Exam,Class VIII


> Regards
> ----- Météo-France -----
> PALAYRET JACQUES
> DCSC/MBD
> [hidden email]
> Fixe : +33 561078319


--
Adrian Klaver
[hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: Removing Last field from CSV string

Michael Nolan
In reply to this post by Alex Magnum


On Sat, May 16, 2020 at 10:19 AM Alex Magnum <[hidden email]> wrote:
Hi,

I have a string that I want to cut to 60 char and then remove the last field and comma.

substring('Class V,Class VI,Class VII,Competitive Exam,Class VIII,Class X,Class XI,Class IX,Class XII',1,60);

substring | Class V,Class VI,Class VII,Competitive Exam,Class VIII,Class

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.

You should be able to write a posix patter that does this, you want to keep everything except a comma followed by 0 or more non-commas and the end of the string boundary to make sure it gets just the last such match.
--
Mike Nolan
Reply | Threaded
Open this post in threaded view
|

Re: Removing Last field from CSV string

Christian Ramseyer
In reply to this post by Alex Magnum


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







Reply | Threaded
Open this post in threaded view
|

Re: Removing Last field from CSV string

Steve Litt
In reply to this post by Alex Magnum
On Sat, 16 May 2020 23:18:57 +0800
Alex Magnum <[hidden email]> wrote:

> Hi,
>
> I have a string that I want to cut to 60 char and then remove the last
> field and comma.
>
> substring('Class V,Class VI,Class VII,Competitive Exam,Class
> VIII,Class X,Class XI,Class IX,Class XII',1,60);
>
> substring | Class V,Class VI,Class VII,Competitive Exam,Class
> VIII*,Class*
>
> 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.
>
> Thank you
> Alex

Assuming the CSV strings are in a file, my first thought would be to
get rid of the final field using AWK, and feed that into your import.
 
SteveT

Steve Litt
May 2020 featured book: Troubleshooting Techniques
     of the Successful Technologist
http://www.troubleshooters.com/techniques


Reply | Threaded
Open this post in threaded view
|

Re: Removing Last field from CSV string

Samuel Roseman
In reply to this post by Alex Magnum
Regular expressions, in my opinion, can be a very powerful text search and replace engine if you know how to use it.
Feel free to enhance what I provided below; it seems to work for the example you provided.

postgres=# select regexp_replace(substring('Class V,Class VI,Class VII,Competitive Exam,Class VIII,Class X,Class XI,Class IX,Class XII',1,60),'(.*),\w+','\1');
                     regexp_replace                     
--------------------------------------------------------
 Class V,Class VI,Class VII,Competitive Exam,Class VIII
(1 row)




Regards,

PflugerGeek

On Saturday, May 16, 2020, 10:19:28 AM CDT, Alex Magnum <[hidden email]> wrote:


Hi,

I have a string that I want to cut to 60 char and then remove the last field and comma.

substring('Class V,Class VI,Class VII,Competitive Exam,Class VIII,Class X,Class XI,Class IX,Class XII',1,60);

substring | Class V,Class VI,Class VII,Competitive Exam,Class VIII,Class

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.

Thank you
Alex