更新时间:2022-11-07 20:09:37
没有直接的功能,但你可以通过替换来实现:
There's no direct function for this, but you can do it with a replace:
declare @myvar varchar(20)
set @myvar = 'Hello World'
select len(@myvar) - len(replace(@myvar,'o',''))
基本上,这会告诉您删除了多少个字符,因此有多少个实例.
Basically this tells you how many chars were removed, and therefore how many instances of it there were.
额外:
以上可以扩展为通过除以正在搜索的字符串的长度来计算多字符字符串的出现次数.例如:
The above can be extended to count the occurences of a multi-char string by dividing by the length of the string being searched for. For example:
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)