Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iUvRh-0004DL-Pk for pgsql-general@arkaria.postgresql.org; Wed, 13 Nov 2019 16:24:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iUvRg-0007bU-EO for pgsql-general@arkaria.postgresql.org; Wed, 13 Nov 2019 16:24:40 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iUvRg-0007bN-3s for pgsql-general@lists.postgresql.org; Wed, 13 Nov 2019 16:24:40 +0000 Received: from evolu-s.it ([94.23.66.144] helo=smtp.evolu-s.it) by magus.postgresql.org with smtp (Exim 4.89) (envelope-from ) id 1iUvRd-0007Hc-Pt for pgsql-general@postgresql.org; Wed, 13 Nov 2019 16:24:39 +0000 Received: from [192.168.1.129] ([79.6.30.247]) by smtp.evolu-s.it ; Wed, 13 Nov 2019 17:24:33 +0100 To: PostgreSQL mailing lists From: Moreno Andreo Subject: CASE(?) to write in a different column based on a string pattern Message-ID: <5bbe8366-bcd4-7f6f-94c8-ebc465c3b6b7@evolu-s.it> Date: Wed, 13 Nov 2019 17:24:40 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.9.1 MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15; format=flowed Content-Transfer-Encoding: 8bit Content-Language: it List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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.-