SQL Repeating Character String

后端 未结 2 1375
旧时难觅i
旧时难觅i 2021-01-27 08:34

Is there a way to evaluate a field if it has repeating values in a cell. For example if someone holds down a number key and it returns some variation of \'00000\' or \'222222222

相关标签:
2条回答
  • 2021-01-27 08:53

    As you are only interested in complete repeatings, such as '3333' and not in, say, '43333', this is rather simple: Find strings longer than one character, where you end up with an empty string when you remove all characters that equal the first one:

    select *
    from mytable 
    where len(value) > 1 and len(replace(value, left(value,1), '')) = 0
    
    0 讨论(0)
  • 2021-01-27 09:16

    The following finds patterns i.e. 333... or 123... or 987...

    Think of it like Rummy 500... Runs and groups of 3's or more.

    Declare @Table table (col int)
    Insert into @Table values
    (4141243),(4290577),(98765432),(78635389),(4141243),(22222),(4290046),(55555555),(4141243),(6789),(77777),(45678),(4294461),(55555),(4141243),(5555)
    
    Declare @Num table (Num int);Insert Into @Num values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9)
    
    Select Distinct A.*
      From @Table A
      Join (
            Select Patt=replicate(Num,3) from @Num
            Union All
            Select Patt=right('000'+cast((Num*100+Num*10+Num)+12 as varchar(5)),3) from @Num where Num<8
            Union All
            Select Patt=reverse(right('000'+cast((Num*100+Num*10+Num)+12 as varchar(5)),3)) from @Num where Num<8
           ) B on CharIndex(Patt,cast(col as varchar(25)))>0
    

    Returns

    col
    5555
    6789
    22222
    45678
    55555
    77777
    55555555
    98765432
    

    Now, If you don't care to identify "runs" (123...)", just remove the following:

        Union All
        Select Patt=right('000'+cast((Num*100+Num*10+Num)+12 as varchar(5)),3) from @Num where Num<8
        Union All
        Select Patt=reverse(right('000'+cast((Num*100+Num*10+Num)+12 as varchar(5)),3)) from @Num where Num<8
    
    0 讨论(0)
提交回复
热议问题