如何计算 SQL varchar 中某个子字符串的出现次数? [英] How do you count the number of occurrences of a certain substring in a SQL varchar?

查看:24
本文介绍了如何计算 SQL varchar 中某个子字符串的出现次数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一列的值格式为 a、b、c、d.有没有办法计算 T-SQL 中该值中逗号的数量?

I have a column that has values formatted like a,b,c,d. Is there a way to count the number of commas in that value in T-SQL?

推荐答案

想到的第一种方法是用空字符串替换逗号并比较长度来间接进行

The first way that comes to mind is to do it indirectly by replacing the comma with an empty string and comparing the lengths

Declare @string varchar(1000)
Set @string = 'a,b,c,d'
select len(@string) - len(replace(@string, ',', ''))

这篇关于如何计算 SQL varchar 中某个子字符串的出现次数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆