我有一张包含以下记录的表
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
答案 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_LIKE
和JOIN
一起使用。
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;