仅仅想知道,在SQL Server中,如何将字符串中的最后一个逗号替换为“and”。
我有以下变量:
@test = 'a,b,c,f,w'
我该如何将字符串中的最后一个逗号替换为“and”作为输出结果呢?
'a,b,c,f and w'
仅仅想知道,在SQL Server中,如何将字符串中的最后一个逗号替换为“and”。
我有以下变量:
@test = 'a,b,c,f,w'
'a,b,c,f and w'
set @test = left(@test, len(@test) - charindex(',', reverse(@test))) + ' and ' + stuff(@test, 1, len(@test) - charindex(',', reverse(@test)) + 1, '')
另一种实现方式:
SELECT reverse(STUFF(reverse(@test), CHARINDEX(',', reverse(@test)), 1, ' dna '))