根据Oracle中的另一列更新列

时间:2017-10-08 03:07:55

标签: sql oracle

我有一张包含以下记录的表

column A   | Column B
19-OCT-16  | SQHDB012384_3~~122
NULL       | SQHDB012384_3~SOHDB006408~122
20-OCT-16  | SQHDB012384_3~~128
NULL       | SQHDB012384_3~SOHDB006408~128

我需要根据SQHDB012384_3 Column A和122 NULL

更新(string before 1st '~') (string after last '~')而非NULL VALUE

更新后的输出应为

column A   | Column B
19-OCT-16  | SQHDB012384_3~~122
19-OCT-16  | SQHDB012384_3~SOHDB006408~122
20-OCT-16  | SQHDB012384_3~~128
20-OCT-16  | SQHDB012384_3~SOHDB006408~128

3 个答案:

答案 0 :(得分:1)

尝试以下UPDATE查询。

UPDATE <tablename> SET Column A = 
(case 
WHEN column B LIKE 'SQHDB012384_3~SOHDB006408~122' then'19-OCT-16'
WHEN column B LIKE 'SQHDB012384_3~SOHDB006408~128' then '20-OCT-16'
ELSE column A
end);

答案 1 :(得分:-1)

我的REGEXP_SUBSTR()版本:

UPDATE tableA A
SET ColumnA = ( SELECT ColumnA FROM tableA B
                WHERE REGEXP_SUBSTR(B.columnB, '^[^~]*~') ||
                      REGEXP_SUBSTR(B.columnB, '~[^~]*$') =
                      REGEXP_SUBSTR(A.columnB, '^[^~]*~') ||
                      REGEXP_SUBSTR(A.columnB, '~[^~]*$') 
WHERE ColumnA IS NULL

答案 2 :(得分:-1)

MERGE INTO与简单REGEXP_LIKEJOIN一起使用。

MERGE INTO mytable r
USING (SELECT b.columnb,
              a.columna new_val
       FROM   mytable a
              inner join mytable b
                      ON Regexp_like (b.columnb,
                         Replace(a.columnb, '~~', '~.+~'))
       WHERE  b.columnb != a.columnb
              AND b.columna IS NULL) m
ON ( m.columnb = r.columnb )
WHEN matched THEN
  UPDATE SET r.columna = m.new_val;