简体   繁体   中英

String loop in T-SQL

How can I do the loop in string.If I want to get number of consecutive addition equals to 1

eg '321' (2-1 counts as 1, 3-2 counts as 1): result 2

eg '320244434321' (2-1 count as 1, 3-2 count as 1 and 4-3 count as 1) result is 3

eg '00321881'(2-1 counts as 1, 3-2 counts as 1): result 2

If I understand correctly, you want the number of adjacent digits that decrease by "1". And you are not counting values twice. So, you can use brute force:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM