Skip to content
Advertisement

Count the number of results in a seperated list

I know I could probably just count the commas but I wanted to know if theres a built in way to count the results in a comma seperated list?

e.g

DECLARE @IDs NVARCHAR(100)
SET @IDS = '2,3,54,234,'

SELECT
  COUNT(value FROM STRING_SPLIT(@IDs)) AS [Count]
FROM
  Table

Advertisement

Answer

Try this:

DECLARE @IDs NVARCHAR(100)
SET @IDS = '2,3,54,234,'

SELECT
    COUNT(*)
FROM 
    STRING_SPLIT(@IDs, ',') 
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement