简体   繁体   中英

Match a Pipe delimited string having column names of different table, once column name match update it as 'GOOD'

Ex. String(column of Table T with name Planets) =' Sun|Earth|Mars'; Table(T) column headers are Planet, Sun, Venus, Mars.

As Mars is common in both, then the value of Mars should be updated as Y in table T.

Table T there are such 50+ columns with which I have to extract and match then update as Y

| pluto |   Sun   |   Earth   |  Mars   |  Planets               |
|-------|---------|-----------|---------|  --------------------- |
|       |   Y     |    Y      |   Y     | Sun|Earth|Mars         |
|  Y    |         |    Y      |         |   Pluto|Earth          |
|       |         |           |         |    (Blank)             |

If the Planet column data is matching with other columns as sun or earth then update those columns as Y.

If I understand well, you may need:

update T
set Pluto   = case when planets like '%Pluto%' then 'Y' end,
    Sun     = case when planets like '%Sun%'   then 'Y' end,
    Earth   = case when planets like '%Earth%' then 'Y' end,   
    Mars    = case when planets like '%Mars%'  then 'Y' end

An example:

SQL> create table T (
  2      Pluto   varchar2(1),
  3      Sun     varchar2(1),
  4      Earth   varchar2(1),
  5      Mars    varchar2(1),
  6      Planets varchar2(100)
  7  );

Table created.

SQL> insert into T (Pluto, Sun, Earth, Mars, Planets) values (null, null, null, null, 'Sun|Earth|Mars');

1 row created.

SQL> insert into T (Pluto, Sun, Earth, Mars, Planets) values (null, null, null, null, 'Pluto|Earth');

1 row created.

SQL> insert into T (Pluto, Sun, Earth, Mars, Planets) values (null, null, null, null, null);

1 row created.

SQL> update T
  2  set pluto   = case when planets like '%Pluto%' then 'Y' end,
  3      Sun     = case when planets like '%Sun%'   then 'Y' end,
  4      Earth   = case when planets like '%Earth%' then 'Y' end,
  5      Mars    = case when planets like '%Mars%'  then 'Y' end;

3 rows updated.

SQL> select * from T;

PLUTO      SUN        EARTH      MARS       PLANETS
---------- ---------- ---------- ---------- --------------------
           Y          Y          Y          Sun|Earth|Mars
Y                     Y                     Pluto|Earth


3 rows selected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM