本文介紹了T-SQL - 按字符比較字符串的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我需要使用 T-SQL 逐字符比較兩個字符串.假設我有兩個這樣的字符串:
I need to compare two strings character by character using T-SQL. Let's assume i have twor strings like these:
123456789
212456789
每次字符不匹配時,我想增加變量@Diff +=1.在這種情況下,前三個字符不同.所以@Diff = 3(0 是默認值).
Every time the character DO NOT match, I would like to increase the variable @Diff +=1. In this case the first three characters differ. So the @Diff = 3 (0 would be default value).
感謝您的所有建議.
推薦答案
對于表中的列你不想使用逐行方法,試試這個:
for columns in table you don't want to use row by row approach, try this one:
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 小提琴演示
這篇關于T-SQL - 按字符比較字符串的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!