简体   繁体   中英

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

I have a question related to a SQL Server query. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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