From: Samuel Roseman <muelrose@yahoo.com>
To: Postgres General <pgsql-general@postgresql.org>
To: Alex Magnum <magnum11200@gmail.com>
Subject: Re: Removing Last field from CSV string
Date: Wed, 20 May 2020 05:21:09 +0000 (UTC)
Message-ID: <759976875.1953487.1589952069934@mail.yahoo.com> (raw)
In-Reply-To: <CA+cR4ze=Qq9V1i4NGNjCP_=O0bduM8KVse1oCVeszsR8LmVyzA@mail.gmail.com>
References: <CA+cR4ze=Qq9V1i4NGNjCP_=O0bduM8KVse1oCVeszsR8LmVyzA@mail.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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: muelrose@yahoo.com, magnum11200@gmail.com
Subject: Re: Removing Last field from CSV string
In-Reply-To: <759976875.1953487.1589952069934@mail.yahoo.com>
References: <CA+cR4ze=Qq9V1i4NGNjCP_=O0bduM8KVse1oCVeszsR8LmVyzA@mail.gmail.com> <759976875.1953487.1589952069934@mail.yahoo.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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