日期:2014-05-18  浏览次数:20562 次

SQL查询一个数字在字段中出现的次数
SQL查询一个数字在字段中出现的次数
例:查询 1 在 13966171891 中出现在次数,这个SQL该如何写呢?

------解决方案--------------------
SQL code

select len(ltrim(col))-len(replace(ltrim(col),'1','')) as cnt
from tb

------解决方案--------------------
SQL code
select len('13966171891')-replace('13966171891','1','')

------解决方案--------------------
SQL code

declare @str varchar(100)
set @str = '13966171891'

select len(ltrim(@str))-len(replace(ltrim(@str),'1',''))

/*************

-----------
4

------解决方案--------------------
SQL code
DECLARE @STR VARCHAR(MAX),@STR_SEARCH VARCHAR(MAX)
SELECT @STR='13966171891',@STR_SEARCH='1'
SELECT COUNT(DISTINCT CHARINDEX(@STR_SEARCH,@STR,NUMBER))
FROM MASTER..SPT_VALUES
WHERE TYPE='P' AND NUMBER BETWEEN 1 AND LEN(@STR)

------解决方案--------------------
SQL code

select len(ltrim('13966171891'))-
len(replace(ltrim('13966171891'),'1',''))
(无列名)
4

------解决方案--------------------

select * from(select len(ltrim(col2))-len(replace(ltrim(col2),'1','')) as cnt
from tb)a where cnt>=3
------解决方案--------------------
SQL code
select len('13966171891')-replace('13966171891','1','')

------解决方案--------------------
select len('13966171891')-replace('13966171891','1','')