我的字符串看起来像:
set @s1 = '#12 + #13 - #14 * 3'
如何查找字符串的所有#XXX部分并将其替换为最终字符串:
hashf('12') + hashf('13') - hash('14') * 3
我尝试使用游标,但是我花了太多时间,出于性能原因,我不想使用游标。
我也试过regex
。模式是"#\d+"
,但我如何在我的案例中应用它?
答案 0 :(得分:0)
我找到了解决方案:
DECLARE @s1 VARCHAR(max)
DECLARE @length INT
DECLARE @current INT
SET @s1 = '#12 + #13 - #14 * 3'
SET @length = Len(@s1)
SET @current = 0
DECLARE @returned_value VARCHAR(max)
WHILE ( @current < @length )
BEGIN
SET @current = Charindex('#', @s1, @current)
SET @s1 = Stuff(@s1, Charindex('#', @s1, @current) , 1, 'func1(''')
SET @s1 = Stuff(@s1, Charindex(' ', @s1, @current) , 1, ''') ')
SET @length = Len(@s1)
SET @current = Charindex('#', @s1, @current)
IF @current = 0
BEGIN
SET @current = @length
END
END
SELECT @s1