From: Moreno Andreo <moreno.andreo@evolu-s.it>
To: PostgreSQL mailing lists <pgsql-general@postgresql.org>
Subject: CASE(?) to write in a different column based on a string pattern
Date: Wed, 13 Nov 2019 17:24:40 +0100
Message-ID: <5bbe8366-bcd4-7f6f-94c8-ebc465c3b6b7@evolu-s.it> (raw)
Hi,
I need to create a CASE (I think) statement to check for a string
pattern, and based on its value, write a substring in a different column
(alias).
I'm trying to create a COPY statement to port a table into antoher
database, which has a table with another format (that's why the aliases)
Let's write it in pseudoSQL:
given this
select pattern from tbl;
pattern
----------
foo1234
bar5678
baz9012
That's what I'm trying to achieve
select
pattern,
CASE when pattern like 'foo%' then ltrim(pattern, 'bar') as foo
when pattern like 'bar%' then ltrim(pattern, 'bar')
as bar
when pattern like 'baz%' then ltrim(pattern, 'baz')
as baz
END
from tbl;
|foo |bar |baz |
1234
5678
9012
(hoping text formatting is ok... 1234 should go in column foo, 568 in
bar and 9012 in baz)
Is it possible?
Thanks in advance
Moreno.-
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: moreno.andreo@evolu-s.it
Subject: Re: CASE(?) to write in a different column based on a string pattern
In-Reply-To: <5bbe8366-bcd4-7f6f-94c8-ebc465c3b6b7@evolu-s.it>
References: <5bbe8366-bcd4-7f6f-94c8-ebc465c3b6b7@evolu-s.it>
* 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