How to count instances of character in SQL Column

前端 未结 16 1750
你的背包
你的背包 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条回答
  •  悲哀的现实
    2020-11-27 13:31

    This snippet works in the specific situation where you have a boolean: it answers "how many non-Ns are there?".

    SELECT LEN(REPLACE(col, 'N', ''))
    

    If, in a different situation, you were actually trying to count the occurrences of a certain character (for example 'Y') in any given string, use this:

    SELECT LEN(col) - LEN(REPLACE(col, 'Y', ''))
    

提交回复
热议问题