How to count instances of character in SQL Column

前端 未结 16 1756
你的背包
你的背包 2020-11-27 12:57

I have an sql column that is a string of 100 \'Y\' or \'N\' characters. For example:

YYNYNYYNNNYYNY...

What is the easiest way

16条回答
  •  旧时难觅i
    2020-11-27 13:18

    This gave me accurate results every time...

    This is in my Stripes field...

    Yellow, Yellow, Yellow, Yellow, Yellow, Yellow, Black, Yellow, Yellow, Red, Yellow, Yellow, Yellow, Black

    • 11 Yellows
    • 2 Black
    • 1 Red
    SELECT (LEN(Stripes) - LEN(REPLACE(Stripes, 'Red', ''))) / LEN('Red') 
      FROM t_Contacts
    

提交回复
热议问题