pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Removing Last field from CSV string
7+ messages / 7 participants
[nested] [flat]

* Removing Last field from CSV string
@ 2020-05-16 15:18  Alex Magnum <magnum11200@gmail.com>
  0 siblings, 4 replies; 7+ messages in thread

From: Alex Magnum @ 2020-05-16 15:18 UTC (permalink / raw)
  To: pgsql-general

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

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Removing Last field from CSV string
@ 2020-05-16 16:31  PALAYRET Jacques <jacques.palayret@meteo.fr>
  0 siblings, 1 reply; 7+ messages in thread

From: PALAYRET Jacques @ 2020-05-16 16:31 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

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 
jacques.palayret@meteo.fr 
Fixe : +33 561078319 

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Removing Last field from CSV string
@ 2020-05-16 16:53  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: PALAYRET Jacques <jacques.palayret@meteo.fr>
  0 siblings, 0 replies; 7+ messages in thread

From: Adrian Klaver @ 2020-05-16 16:53 UTC (permalink / raw)
  To: PALAYRET Jacques <jacques.palayret@meteo.fr>; pgsql-general@lists.postgresql.org

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
> jacques.palayret@meteo.fr
> Fixe : +33 561078319


-- 
Adrian Klaver
adrian.klaver@aklaver.com





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Removing Last field from CSV string
@ 2020-05-16 17:29  Michael Nolan <htfoot@gmail.com>
  parent: Alex Magnum <magnum11200@gmail.com>
  3 siblings, 0 replies; 7+ messages in thread

From: Michael Nolan @ 2020-05-16 17:29 UTC (permalink / raw)
  To: Alex Magnum <magnum11200@gmail.com>; +Cc: pgsql-general

On Sat, May 16, 2020 at 10:19 AM Alex Magnum <magnum11200@gmail.com> 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
nolan@tssi.com

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Removing Last field from CSV string
@ 2020-05-16 19:59  Christian Ramseyer <rc@networkz.ch>
  parent: Alex Magnum <magnum11200@gmail.com>
  3 siblings, 0 replies; 7+ messages in thread

From: Christian Ramseyer @ 2020-05-16 19:59 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org; magnum11200@gmail.com



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










^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Removing Last field from CSV string
@ 2020-05-18 02:07  Steve Litt <slitt@troubleshooters.com>
  parent: Alex Magnum <magnum11200@gmail.com>
  3 siblings, 0 replies; 7+ messages in thread

From: Steve Litt @ 2020-05-18 02:07 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On Sat, 16 May 2020 23:18:57 +0800
Alex Magnum <magnum11200@gmail.com> 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





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Removing Last field from CSV string
@ 2020-05-20 05:21  Samuel Roseman <muelrose@yahoo.com>
  parent: Alex Magnum <magnum11200@gmail.com>
  3 siblings, 0 replies; 7+ messages in thread

From: Samuel Roseman @ 2020-05-20 05:21 UTC (permalink / raw)
  To: pgsql-general; Alex Magnum <magnum11200@gmail.com>

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 <magnum11200@gmail.com> 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 youAlex  

^ permalink  raw  reply  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2020-05-20 05:21 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-05-16 15:18 Removing Last field from CSV string Alex Magnum <magnum11200@gmail.com>
2020-05-16 17:29 ` Michael Nolan <htfoot@gmail.com>
2020-05-16 19:59 ` Christian Ramseyer <rc@networkz.ch>
2020-05-18 02:07 ` Steve Litt <slitt@troubleshooters.com>
2020-05-20 05:21 ` Samuel Roseman <muelrose@yahoo.com>
2020-05-16 16:31 Re: Removing Last field from CSV string PALAYRET Jacques <jacques.palayret@meteo.fr>
2020-05-16 16:53 ` Adrian Klaver <adrian.klaver@aklaver.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox