最大连续出现在文本字符串中

Vik*_*ram -2 sql sql-server

请帮我查询以确定文本字符串SQL Server中连续出现的次数.

Sample Date     | Query Output
---------------------------
11001           | 2
10000           | 1
11111           | 5
10111           | 3
00000           | 0
10010           | 1
10001           | 1
00100           | 1
11001           | 2
10100           | 1
01110           | 3
00011           | 2
00000           | 0
10010           | 1
01111           | 4
00110           | 2
10011           | 2
10010           | 1
01101           | 2
01010           | 1
10101           | 1
Run Code Online (Sandbox Code Playgroud)

Gor*_*off 7

我认为,最简单的方法是caselike:

select (case when col like '%11111%' then 5
             when col like '%1111%' then 4
             when col like '%111%' then 3
             when col like '%11%' then 2
             when col like '%1%' then 1
             else 0
         end)
Run Code Online (Sandbox Code Playgroud)

毫无疑问,有更聪明的方法.但如果有一种更简单的方法,我会感到惊讶.

  • *毫无疑问 - 更聪明的方法* - 哦不!这*是一个非常聪明的方法! (2认同)