我有一张包含以下数据集的表
ID ReportID RecIDs
7083 52682 47407,47505,33648,47412,
4419 52749 47412,47218,36636,47866,47669,47505,47259,
7496 52883 47488,
6547 52936 47345,47236,47407,33648,47512,47412,47254,47274,36636,
6225 53066 47412,36636,47236,47254,47367,33648,
2733 53067 47218,47221,47236,33648,47866,36636,33595,39485,
6923 53084 37061,47412,47407,47376,47274,47505,47651,47820,
我想编写一个查询,它将为我提供RedIDs列中所有逗号分隔条目的计数。 就像行id 6923
的RecID值的计数一样答案 0 :(得分:3)
您可以使用REPLACE
:
SELECT ID, ReportID, RecIDs,
LEN(RecIDs) - LEN(REPLACE(RecIDs, ',', '')) AS cnt
FROM mytable
WHERE ID = 6923
这会计算','
列中RecIDs
的数量。这应该等于其中的数字。
答案 1 :(得分:0)
SELECT SUM(len(RecIDs) - len(replace(RecIDs, ',', '')) +1)
AS RedIDs
FROM DBTABLE