根据另一列中的值拆分列中的数据

问题描述

我有一张充满艺术家和曲目标题的表,该表是从两个不同的地方导入的。

“艺术家”和“标题”在不同的列中是正确的,但是在其他行中,“标题”列中的所有内容均以“-”分隔,“艺术家”列为NULL

enter image description here

在源代码处更改它不是一种选择,因为两种数据格式都位于同一日志文件中。

我想我需要在“艺术家”列中获得所有带有NULL值的行,然后使用STRING_SPLIT拆分“标题”列。我的数据库是SQL Server 2019。

我曾尝试在互联网上修改许多不同的示例,但是没有什么能奏效的。

谢谢您的帮助!

解决方法

您可以将其作为选择查询:

select t.*,(case when artist is null and title like '%-%'
             then left(title,charindex('-',title) - 1)
             else artist
        end) as imputed_artist,(case when artist is null and title like '%-%'
             then stuff(title,1,title) + 1,'')
             else title
        end) as imputed_title
from t;

类似的逻辑可以应用于update

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...