简体   繁体   English

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

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

I have a question related to a SQL Server query. 我有一个与SQL Server查询有关的问题。 I want to count all '//' in a dataset. 我想计算数据集中的所有“ //”。 The dataset contains a code. 数据集包含一个代码。 Through counting the '//' i want to declare it as a Comment, IF EVERY SINGLE ROW contains at least one time the string '//'. 通过计数“ //”,我想将其声明为注释,如果每个单行至少包含一次字符串“ //”。

Actually I've count the amount of '//' in a dataset, but i don't know how should i continue. 实际上,我已经计算了数据集中的“ //”数量,但是我不知道该如何继续。

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

You are going to get twice the count. 您将获得两倍的计数。 So either divide by 2 or: 因此,除以2或:

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

Sorry for double-posting but I solved the issue on my own :-). 对不起,我要重复发布,但是我自己解决了这个问题:-)。 Code is... 代码是...

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