How to count instances of character in SQL Column

前端 未结 16 1631
你的背包
你的背包 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:43

    Here's what I used in Oracle SQL to see if someone was passing a correctly formatted phone number:

    WHERE REPLACE(TRANSLATE('555-555-1212','0123456789-','00000000000'),'0','') IS NULL AND
    LENGTH(REPLACE(TRANSLATE('555-555-1212','0123456789','0000000000'),'0','')) = 2
    

    The first part checks to see if the phone number has only numbers and the hyphen and the second part checks to see that the phone number has only two hyphens.

提交回复
热议问题