gpt4 book ai didi

sql - T-SQL - 逐字符比较字符串

转载 作者:行者123 更新时间:2023-12-01 09:28:49 25 4
gpt4 key购买 nike

我需要使用 T-SQL 逐个字符地比较两个字符串。假设我有两个这样的字符串:

123456789    
212456789

每次字符不匹配时,我想增加变量@Diff +=1。在这种情况下,前三个字符不同。所以@Diff = 3(0 是默认值)。

感谢您的所有建议。

最佳答案

对于表格中您不想使用逐行方法的列,试试这个:

with cte(n) as (
select 1
union all
select n + 1 from cte where n < 9
)
select
t.s1, t.s2,
sum(
case
when substring(t.s1, c.n, 1) <> substring(t.s2, c.n, 1) then 1
else 0
end
) as diff
from test as t
cross join cte as c
group by t.s1, t.s2

=>sql fiddle demo

关于sql - T-SQL - 逐字符比较字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18575639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com