How to count instances of character in SQL Column

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

    You can also Try This

    -- DECLARE field because your table type may be text
    DECLARE @mmRxClaim nvarchar(MAX) 
    
    -- Getting Value from table
    SELECT top (1) @mmRxClaim = mRxClaim FROM RxClaim WHERE rxclaimid_PK =362
    
    -- Main String Value
    SELECT @mmRxClaim AS MainStringValue
    
    -- Count Multiple Character for this number of space will be number of character
    SELECT LEN(@mmRxClaim) - LEN(REPLACE(@mmRxClaim, 'GS', ' ')) AS CountMultipleCharacter
    
    -- Count Single Character for this number of space will be one
    SELECT LEN(@mmRxClaim) - LEN(REPLACE(@mmRxClaim, 'G', '')) AS CountSingleCharacter
    

    Output:

提交回复
热议问题