Search Postgresql Archives

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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.-






[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]

  Powered by Linux