小编Dav*_*ips的帖子

根据其他列值进行条件替换

我有一个包含表的数据库:

********************************
* Code  *      FileName        *
********************************
* NULL  * Cats and Dogs        *
* C123  * C123 - Cats and Dogs *
* NULL  * Baking Cakes         *
* Z345  * Z345 - Plants        *
* F967  * E345 - Tractors      *
********************************
Run Code Online (Sandbox Code Playgroud)

我想返回所有行的文件名或操纵文件名,基于代码列中是否有值并且它与文件名中的代码匹配。

所以查询应该返回

Cats and Dogs
xxxx - Cats and Dogs
Baking Cakes
xxxx - Plants
E345 - Tractors
Run Code Online (Sandbox Code Playgroud)

从上面的一组数据来看。

我正在努力对另一列中的值进行条件替换 - 如果我使用 case 语句进行替换,我需要列出所有可能的代码,这将很难维护。有什么办法吗

Select Replace(FileName, Code, "xxxx") from table where filename like %Code%
Run Code Online (Sandbox Code Playgroud)

sql conditional replace

1
推荐指数
1
解决办法
2724
查看次数

标签 统计

conditional ×1

replace ×1

sql ×1