[英]Find values in between two characters '(' and ')' in column that have nulls
为了您的数据,我将执行以下操作:
select coalesce(replace(stuff(name, 1, charindex('(', name + '(') + 1, ''), ')', ''),
'') as [Name]
像示例中一样,假定括号在字符串的末尾。
在这里,适合您的情况
declare @string varchar(25)
set @string = '(asdfgh)'
SELECT SUBSTRING(@string,
charindex('(', @string)+1,
charindex(')', @string)-
charindex('(', @string)-1)
您可以使用APPLY
:
select substring(name, tt.startp + 1, (endp - startp) - 1) as Name
from Authors a cross apply
( values (charindex('(', name + ')'), charindex(')', name + ')'))
) tt(startp, endp);
您的“ Name
列在某些地方没有(
... )
,因此,您可以添加where
子句或在字符串末尾显式添加(
... )
。
declare @string nvarchar(25)
设置@string ='(asdfgh)'
选择REPLACE(REPLACE(@string,'(',''),')','')其中@string如'%[(] %% [)]%'
在上面的代码中,我使用正则表达式在(和)之间查找字符串,然后将其替换为空
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.