原贴: 点击打开链接
DECLARE @s1 VARCHAR(20),@s2 VARCHAR(20)
SET @s1='123X56X89'
SET @s2='12XX56789'
;WITH tt1 AS (
SELECT sv.number AS id,SUBSTRING(t1.c,sv.number,1) AS c
FROM (SELECT @s1 AS c)AS t1 CROSS APPLY [master].dbo.spt_values AS sv
WHERE sv.[type]='P' AND sv.number BETWEEN 1 AND LEN(t1.c)
), tt2 AS (
SELECT sv.number AS id,SUBSTRING(t2.c,sv.number,1) AS c
FROM (SELECT @s2 AS c)AS t2 CROSS APPLY [master].dbo.spt_values AS sv
WHERE sv.[type]='P' AND sv.number BETWEEN 1 AND LEN(t2.c)
)
SELECT (
SELECT
CASE WHEN a.c=b.c THEN a.c
WHEN a.c='X' THEN b.c
ELSE a.c END
FROM tt1 AS a INNER JOIN tt2 AS b ON a.id=b.id
FOR XML PATH('')
) AS result
/*
result
123X56789
*/