22,209
社区成员
发帖
与我相关
我的任务
分享
SELECT '1' AS FLAG ,COUNT(1) AS 统计
FROM LI
WHERE CHARINDEX('1',COL1) > 0
Union all
SELECT 'a' AS FLAG ,COUNT(1) AS 统计
FROM LI
WHERE CHARINDEX('a',COL1) > 0
union all
SELECT 'g' AS FLAG ,COUNT(1) AS 统计
FROM LI
WHERE CHARINDEX('g',COL1) > 0
select [1]=sum(case when charindex('1',col)>0 then 1 else 0 end),
[a]=sum(case when charindex('a',col)>0 then 1 else 0 end),
[g]=sum(case when charindex('g',col)>0 then 1 else 0 end)
from tb
select [1]=case when charindex('1',col)>0 then 1 else 0 end,
[a]=case when charindex('a',col)>0 then 1 else 0 end,
[g]=case when charindex('g',col)>0 then 1 else 0 end
from tb
select
sum(case when charindex('1',列)>0 then 1 else 0 end) as [包含1的行数],
sum(case when charindex('a',列)>0 then 1 else 0 end) as [包含a的行数],
sum(case when charindex('g',列)>0 then 1 else 0 end) as [包含g的行数]
from
tb
--得到一个字符串在另一个字符串中出现的次数
create function f_getcharcount(
@str varchar(8000),
@chr varchar(20)
) returns int
as
begin
declare @re int,@i int
select @re=0,@i=charindex(@chr,@str)+1
while @i>1
select @re=@re+1
,@str=substring(@str,@i,8000)
,@i=charindex(@chr,@str)+1
return(@re)
end
go
--调用示例
select dbo.f_getcharcount('aadddbbbbad','ad')