融合两个字符串

原贴: 点击打开链接

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
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值