要求是这样的:
有一个表TableA,里面包含列 Col1,Col2,
现在想要查询 处Col1和Col2的数据合并到一列,
假设表数据如下
Col1 Col2
aa1 bb1
aa2 bb2
结果集如下
Col3
aa1_bb1
aa2_bb2
其中如果Col1对应的字段值为空白字符串“ ”或者为null则只返回Col2的值,否则返回Col1_Col2
我写的sql语句如下:
select case
when ( nvl(Col1, '') = '' or
length(trim(RESERVE2)) = '0') then
Col2
else
(Col1|| '_' || Col2)
end as Col3
from TableA
好像case when 跟的条件有错......
------解决思路----------------------
with tmp(col1,col2) as
(select 'aa1','bb1' from dual union all
select ' ','bb2' from dual union all
select '','bb3' from dual union all
select null,'bb4' from dual)
select case when trim(col1) is null then col2 else col1
------解决思路----------------------
'_'
------解决思路----------------------
col2 end as col3 from tmp
------解决思路----------------------
with tmp(col1,col2) as
(select 'aa1','bb1' from dual union all
select ' ','bb2' from dual union all
select '','bb3' from dual union all
select null,'bb4' from dual)
select case when trim(col1) is not null and trim(col2) is not null then col1
------解决思路----------------------
'_'
------解决思路----------------------
col2 else concat(col1,col2) end as col3 from tmp
只需考虑两者均非空情形时要加下划,其他的交给concat()函数即可