计算varchar中varchar出现的最佳方法是什么。
我宁愿不遍历文本来查找某些组合。
此选择仅找到第一个
SELECT CASE WHEN CHARINDEX('!','HOW MANY TIMES IS ! IN THIS TEXT ? THIS IS MY QUESTION !' ) > 0 THEN 1 ELSE 0 END
返回1
我需要一种方法来找到匹配的总数
TABLE DATA SEARCHTEXT LONGTEXT ! HOW MANY TIMES IS ! IN THIS TEXT ? THIS IS MY QUESTION ! HELLO HELLO HELLO HELLO HELLO HELLO HELLO L HELLO HELLO HELLO HELLO HELLO HELLO e more testdata
预期结果
Count SEARCHTEXT LONGTEST 2 ! MANY TIMES IS ! IN THIS TEXT ? THIS IS MY QUESTION ! 6 HELLO HELLO HELLO HELLO HELLO HELLO HELLO 12 L HELLO HELLO HELLO HELLO HELLO HELLO 2 e more testdata
使用MSSQL Server 2005
您可以replace使每次出现的时间延长1个字符,并计算增加的字符数:
replace
select len(replace(txt, search, search + '#')) - len(txt) as Count , * from YourTable
SE Data的完整示例。