在 SQL Server 2008 中删除 () 包括 () 之间的字符串

Remove string between () including () in SQL Server 2008

我有一个 name 列,其中一些单元格中包含一个括号内的字符串。

示例:

Smith (Divorced)
Jones
Janes
Renold (Deaceased)...

等等。我需要完全删除括号中的部分。

Smith
Jones
Janes
Renold

尝试了各种 CHARINDEXREPLACE,但要么出现无效长度错误,要么只删除了一部分。

这是您基本需要的,只需修改它以适合您的查询:

declare @tmp table (name varchar(100))
insert @tmp values ('smith (divorced)' ) , ('jones'), ('renold (deceased)...')

select name
    , case 
        when charindex('(', name, 1) > 0 
            then rtrim(left(name, charindex('(', name, 1) - 1)) 
        else name 
      end as [name]
from @tmp

如果您需要替换现有数据,只需发出 UPDATE,如下所示:

UPDATE Persons_Table
SET Name = case 
            when charindex('(', Name, 1) > 0 
                then rtrim(left(Name, charindex('(', Name, 1) - 1)) 
            else Name 
          end
WHERE charindex('(', Name, 1) > 0 -- could prove useful since you might not want to go
                                  -- over all of the data