CREATE FUNCTION [dbo].[FN_Remove_Bracket] (@str NVARCHAR(1000) --要转换的字符串
)
RETURNS NVARCHAR(1000)
AS
BEGIN
declare @left int,@right int,@use int,@rtn nvarchar(1000),@temp_l nvarchar(1000),@temp_r nvarchar(1000)
set @rtn = @str
set @left =len(@rtn) - len(REPLACE(@rtn,'(',''))
set @right =len(@rtn) - len(REPLACE(@rtn,')',''))
set @use = 0
if @left >= @right
begin
set @use = @right
end
else
begin
set @use = @left
end
--123456(78)9101112
while @use > 0
begin
declare @l int,@r int
set @l = 0 --查找左括号位置
set @r = 0 --查找右括号位置
set @r = charindex(')',@rtn)
--)87(654321
set @temp_l =REVERSE( SUBSTRING(@rtn,1,@r))
set @l = charindex('(',@temp_l)
while @l = 0
begin
if @r+1 > len(@rtn)
break
set @r = charindex(')',@rtn, @r+1)
if @r = 0
break
set @temp_l =REVERSE( SUBSTRING(@rtn,1,@r))
set @l = charindex('(',@temp_l)
end
----(78)
set @temp_l = REVERSE(SUBSTRING(@temp_l,1,@l))
set @rtn = REPLACE(@rtn,@temp_l,'')
set @use = @use - 1
end
RETURN (@rtn)
END
sqlserver 去掉括号和括号里的内容,支持去掉多对括号
于 2022-10-06 17:49:03 首次发布