您可以使用:
演示
DECLARE
@String1 NVARCHAR(100) = '***ABCABCABCABCABC***',
@String2 NVARCHAR(100) = 'BBBTTTTTTTTTTTTTTTCCC';
DECLARE @new_string NVARCHAR(100) = NULL;
;WITH nums AS
(
SELECT TOP 1000 ROW_NUMBER() OVER(ORDER BY t1.number) AS N
FROM master..spt_values t1
CROSS JOIN master..spt_values t2
), string_1 AS
(
SELECT N, letter
FROM nums
CROSS APPLY (SELECT SUBSTRING(@String1, nums.N, 1) WHERE nums.N <= LEN(@String1))
AS t(letter)
), string_2 AS
(
SELECT N, letter
FROM nums
CROSS APPLY (SELECT SUBSTRING(@String2, nums.N, 1) WHERE nums.N <= LEN(@String2))
AS t(letter)
), new_string AS
(
SELECT Letter = CASE WHEN s1.Letter <> '*' THEN s1.Letter ELSE s2.Letter END,
s1.N
FROM string_1 s1
LEFT JOIN string_2 s2
ON s1.N = s2.N
)
SELECT @new_string = (SELECT '' + ISNULL(Letter, '')
FROM new_string
ORDER BY N ASC
FOR XML PATH(''));
SELECT @new_string
UNION ALL
SELECT @String2;
生成数字(您可以使用任何方法)
按字母拆分字符串一和二
生成新字符串,替换*
为对应的字母
将字母连接在一起
备注:您可以将所有这些组合在一个查询中,但为了便于阅读,我将其拆分。
随意包装它以使其优雅:
[dbo].[udf_merge_string](@string NVARCHAR(100), @string2 NVARCHAR(100)) ...