簡體   English   中英

T-SQL 中的字符串循環

[英]String loop in T-SQL

如何在字符串中進行循環。如果我想獲得連續加法的數量等於 1

例如'321'(2-1 計為 1,3-2 計為 1):結果 2

例如'320244434321'(2-1 計為 1,3-2 計為 1,4-3 計為 1)結果為 3

例如'00321881'(2-1算1,3-2算1):結果2

如果我理解正確,您想要減少“1”的相鄰數字的數量。 而且您不會兩次計算值。 因此,您可以使用蠻力:

select ( (case when str like '%10%' then 1 else 0 end) +
         (case when str like '%21%' then 1 else 0 end) +
         (case when str like '%32%' then 1 else 0 end) +
         (case when str like '%43%' then 1 else 0 end) +
         (case when str like '%54%' then 1 else 0 end) +
         (case when str like '%65%' then 1 else 0 end) +
         (case when str like '%76%' then 1 else 0 end) +
         (case when str like '%87%' then 1 else 0 end) +
         (case when str like '%98%' then 1 else 0 end)
        )
from t;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM