繁体   English   中英

计算数据集中“ //”的数量(SQL Server)

[英]Counting amount of '//' in a dataset (SQL Server)

我有一个与SQL Server查询有关的问题。 我想计算数据集中的所有“ //”。 数据集包含一个代码。 通过计数“ //”,我想将其声明为注释,如果每个单行至少包含一次字符串“ //”。

实际上,我已经计算了数据集中的“ //”数量,但是我不知道该如何继续。

select col1, col2, LEN(col3) - LEN(REPLACE(col3, '//', '' )) AS col3counter
from #tmp

您将获得两倍的计数。 因此,除以2或:

select col1, col2,
       ( LEN(col3) - LEN(REPLACE(col3, '//', 'x' )) ) AS col3counter
from #tmp

对不起,我要重复发布,但是我自己解决了这个问题:-)。 代码是...

select col1, col2, col3, col4,
    LEN(col3) - LEN(REPLACE(col3, '//', ' ' )) AS col3commentcount,
    LEN(col3) - LEN(Replace(col3,(char(13)+char(10)),' '))+1 as  col3rowcount,
    LEN(col4) - LEN(REPLACE(col4, '//', ' ')) AS col4commentcount,
    LEN(col4) - LEN(REPLACE(col4,(char(13)+char(10)),' '))+1 as col4rowcount
from #tmp
where LEN(col3) - LEN(REPLACE(col3, '//', ' ' )) = 
  LEN(col3) - LEN(Replace(col3,(char(13)+char(10)),' '))+1 OR
  LEN(col4) - LEN(REPLACE(col4, '//', ' ')) =
  LEN(col4) - LEN(REPLACE(col4,(char(13)+char(10)),' '))+1
  and idcheck=0  
order by col1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM