[英]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.