根据Redshift sql中另一列的条件从列中删除字符

时间:2018-06-06 15:03:18

标签: sql amazon-redshift

我有一张桌子,我想要做以下事情: 如果col_1的值为“sakc”或“cosc”,则从col_2的那些行中删除出现的字符“_”。

示例:

鉴于table_1

col_1            col_2

sakc             abc_aw
sakc             asw_12
cosc             absd12
dasd             qwe_32
cosc             dasd_1

所需的table_1

col_1            col_2

sakc             abcaw
sakc             asw12
cosc             absd12
dasd             qwe_32
cosc             dasd1   

我尝试使用以下内容:

select case when col_1 in ('sakc', 'cosc') then trim("_" from col_2) end col_2 from table_1;

但我确信这不是正确的方法并且给我错误。

1 个答案:

答案 0 :(得分:4)

您可以使用replace()

SELECT  
  col_1
 ,CASE 
    WHEN col_1 in ('sakc', 'cosc') THEN REPLACE(col_2, '_', '')
    ELSE col_2 
  END col2 
FROM table_1;
相关问题