删除SQLServer中的括号
我试图删除括号内的单词,以及 SQL Server 中的括号(来自列 Col1 中的多个值)。我已经查看了关于此的其他帖子,但没有成功。
我尝试过的:
SELECT
left(Col1, charindex('(', Col1) - 1)
FROM
Table;
也
declare @str varchar(100)
set @str='Col1'
select left(@str, charindex('(', @str)-1)
from Table
回答
SQL Server 具有糟糕的字符串操作功能。如果您想要第一个(然后在第一个之后的所有内容),您可以使用:
select (case when col like '%(%)%'
then concat(left(col, charindex('(', col) - 1),
stuff(col, 1, charindex(')', col), '')
)
else col
end)
您可以使用相同的逻辑更新表:
update t
set col = concat(left(col, charindex('(', col) - 1),
stuff(col, 1, charindex(')', col), '')
)
where col like '%(%)%';