簡體   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