如果满足A列中的条件,则用B列的值填充A列中的行 [英] Fill rows in column A with value of column B if condition in column A is met
本文介绍了如果满足A列中的条件,则用B列的值填充A列中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一张桌子,像这样:
I have a table like:
colA | colB
" " | 1
"K 111" | 1
"K222" | 2
" " | 3
有些列只有一个空格("),有些列具有"K {number}",有些列则具有"K {number}".
Some columns have only a space (" "), some have "K {number}", some have "K{number}".
如果colA有空格,我希望将该值替换为colB中的值.
If colA has a space I want that value replaced with the one from colB.
所以最终结果应该是:
colA | colB
1 | 1
"K abc" | 1
"Kdef" | 2
3 | 3
我该怎么做?
推荐答案
您可以使用 case
表达式:
select (case when colA = ' ' then to_char(col_b)
else colA
end) as new_colA
如果想更笼统,可以使用 like
:
If you wanted to be more general, you might use like
:
select (case when colA like 'K%' then colA
else
end) as new_colA
在 update
中,您可以将 when
条件移至过滤条件:
In an update
, you would move the when
condition to a filtering condition:
update t
set colA = to_char(colb)
where colA = ' ';
这篇关于如果满足A列中的条件,则用B列的值填充A列中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文