Re: CASE(?) to write in a different column based on a string pattern

From: Geoff Winkless <pgsqladmin(at)geoff(dot)dj>
To: Moreno Andreo <moreno(dot)andreo(at)evolu-s(dot)it>
Cc: PostgreSQL mailing lists <pgsql-general(at)postgresql(dot)org>
Subject: Re: CASE(?) to write in a different column based on a string pattern
Date: 2019-11-13 16:36:43
Message-ID: CAEzk6ff14JeGb0MtbGZLq2vSJ7SrnNE6FzABsRrkb_n592E-YQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, 13 Nov 2019 at 16:24, Moreno Andreo <moreno(dot)andreo(at)evolu-s(dot)it> wrote:
> |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?

Simplest way in plain SQL would be individual case statements for each
column, I think.

SELECT pattern,
CASE WHEN pattern LIKE 'foo%' THEN SUBSTR(pattern, 4) ELSE '' END AS foo
CASE WHEN pattern LIKE 'bar%' THEN SUBSTR(pattern, 4) ELSE '' END AS bar
CASE WHEN pattern LIKE 'baz%' THEN SUBSTR(pattern, 4) ELSE '' END AS baz
FROM tbl;

Geoff

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Andrew Kerber 2019-11-13 16:48:36 Re: CASE(?) to write in a different column based on a string pattern
Previous Message Moreno Andreo 2019-11-13 16:24:40 CASE(?) to write in a different column based on a string pattern