Program Tip

특정 문자가 문자열에 나타나는 횟수

programtip 2020. 11. 16. 22:04
반응형

특정 문자가 문자열에 나타나는 횟수


특정 문자가 문자열에 나타나는 횟수를 계산하는 MS SQL Server 기능이 있습니까?


이에 대한 직접적인 기능은 없지만 대체로 수행 할 수 있습니다.

declare @myvar varchar(20)
set @myvar = 'Hello World'

select len(@myvar) - len(replace(@myvar,'o',''))

기본적으로 이것은 얼마나 많은 문자가 제거되었는지, 따라서 얼마나 많은 인스턴스가 제거되었는지 알려줍니다.

특별한:

위의 내용은 검색되는 문자열의 길이로 나누어 다중 문자 문자열의 발생 횟수를 계산하도록 확장 할 수 있습니다. 예를 들면 :

declare @myvar varchar(max), @tocount varchar(20)
set @myvar = 'Hello World, Hello World'
set @tocount = 'lo'

select (len(@myvar) - len(replace(@myvar,@tocount,''))) / LEN(@tocount)

시퀀스를 바꾼 후 문자열의 길이를 확인하십시오.

declare @s varchar(10) = 'aabaacaa'
select len(@s) - len(replace(@s, 'a', ''))
>>6

replace을 사용하여 수행 할 수 있습니다 len.

x문자 수 계산 str:

len(str) - len(replace(str, 'x', ''))

그것을 시도하십시오 :

declare @t nvarchar(max)
set @t='aaaa'

select len(@t)-len(replace(@t,'a',''))

SQL SERVER 2016부터이 기능 사용

Select Count(value) From STRING_SPLIT('AAA AAA AAA',' ');

-- Output : 3 

이 함수를 카운트 함수와 함께 사용하면 문자열에 몇 개의 문자가 있는지 알려줍니다.


SQL 서버의 기능 :

CREATE function NTSGetCinC(@Cadena nvarchar(4000), @UnChar nvarchar(100)) 
Returns int 

 as  

 begin 

 declare @t1 int 

 declare @t2 int 

 declare @t3 int 

 set @t1 = len(@Cadena) 

 set @t2 = len(replace(@Cadena,@UnChar,'')) 

 set @t3 = len(@UnChar) 


 return (@t1 - @t2)  / @t3 

 end 

Code for visual basic and others:

Public Function NTSCuentaChars(Texto As String, CharAContar As String) As Long

NTSCuentaChars = (Len(Texto) - Len(Replace(Texto, CharAContar, ""))) / Len(CharAContar)

End Function

You may do this completely in-line by replacing the desired character with an empty string, calling LENGTH function and substracting from the original string's length.

SELECT 
  CustomerName, 
  LENGTH(CustomerName) -
  LENGTH(REPLACE(CustomerName, ' ', '')) AS NumberOfSpaces
FROM Customers;

Use this code, it is working perfectly. I have create a sql function that accept two parameters, the first param is the long string that we want to search into it,and it can accept string length up to 1500 character(of course you can extend it or even change it to text datatype). And the second parameter is the substring that we want to calculate the number of its occurance(its length is up to 200 character, of course you can change it to what your need). and the output is an integer, represent the number of frequency.....enjoy it.


CREATE FUNCTION [dbo].[GetSubstringCount]
(
  @InputString nvarchar(1500),
  @SubString NVARCHAR(200)
)
RETURNS int
AS
BEGIN 
        declare @K int , @StrLen int , @Count int , @SubStrLen int 
        set @SubStrLen = (select len(@SubString))
        set @Count = 0
        Set @k = 1
        set @StrLen =(select len(@InputString))
    While @K <= @StrLen
        Begin
            if ((select substring(@InputString, @K, @SubStrLen)) = @SubString)
                begin
                    if ((select CHARINDEX(@SubString ,@InputString)) > 0)
                        begin
                        set @Count = @Count +1
                        end
                end
                                Set @K=@k+1
        end
        return @Count
end

BEST

DECLARE @yourSpecialMark = '/';
select len(@yourString) - len(replace(@yourString,@yourSpecialMark,''))

It will count, how many times occours the special mark '/'

참고URL : https://stackoverflow.com/questions/9789225/number-of-times-a-particular-character-appears-in-a-string

반응형