我有一个具有硬编码值的变量
customers.name ='施密特'
declare @value as nvarchar(max)
set @value = 'customers.name = ''Schmidt'''
我想以这种方式改变@value
customers.last_name ='施密特'
如何使用查询完成此操作? 提前谢谢。
答案 0 :(得分:2)
DECLARE @Value AS NVARCHAR(MAX) = 'customers.name = ''Schmidt'''
,@Replacement AS NVARCHAR(MAX) = 'last_name'
,@SearchString AS NVARCHAR(MAX) = 'name'
SET @Value = REPLACE(@value, @SearchString, @Replacement)
PRINT @Value;
答案 1 :(得分:1)
您应该将customers.name
替换为customers.last_name
。
DECLARE @text VARCHAR(200) = 'customers.name = ''Schmidt'''
SELECT
OriginalText = @text,
ReplacedText = REPLACE(@text, 'customers.name', 'customers.last_name')
/*
Result:
OriginalText: customers.name = 'Schmidt'
ReplacedText: customers.last_name = 'Schmidt'
*/
一般来说,在更换琴弦时,它们越大,错误更换的机会就越低。如果您确定该值始终以customers.name =
开头,则应将其替换为customers.last_name =
。如果您尝试简单地替换name
,则最终可能会在另一个字符串出现时替换它。