sql - 根据Redshift sql中另一列的条件从列中删除字符
问题描述
我有一个表,我想在其中执行以下操作:如果 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
所需的表_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;
但我确信这不是正确的方法并且给了我错误。
解决方案
您可以使用replace()
SELECT
col_1
,CASE
WHEN col_1 in ('sakc', 'cosc') THEN REPLACE(col_2, '_', '')
ELSE col_2
END col2
FROM table_1;
推荐阅读
- mysql - 如何构建数据库逻辑?
- java - 如何使用 Hibernate 在 Oracle 序列上实现持久计数器?
- matlab - 匿名函数中的 if-then-else
- typescript - 我怎么能有正确的类型?
- haskell - 替换列表中的元素
- asp.net-core - kendo UI 是否使用捆绑的 jQuery?
- c# - Unity WebGL:为什么使用下载的 Mp3 或 OGG 文件,无法播放音频?
- javascript - 如何干扰 htaccess 重定向 url 以检查访问者是否登录了 wordpress?
- jquery - 如何将我的 http 请求的 CSS/Jquery 文件更改为可在 Rails PROD 环境中使用的文件?
- python - 从文本文件分配时如何摆脱 '\n' 作为 dict 值的一部分