Question: Is there a way in SQL server to create a function to have the following string 'ABC,DEF,GHI' output the value 3.

And then is it possible to have a function that would allow you to specify what token of the string you want output.
So input would be the string and the token you want returned.
input: ('ABC,DEF,GHI', 2)
output: DEF

Answer: 
The first question

```
declare @token varchar(20)
set @token = 'ABC,DEF,GHI'

select len(@token) - len(replace(@token ,',','')) + 1

```
This function does what I need for part 2

```
CREATE FUNCTION dbo.itemAtIndexInASplitString( @stringToSplit VARCHAR(MAX), @delimiter VARCHAR(5), @indexToReturn int)
RETURNS
varchar(max)
AS
BEGIN
DECLARE @returnList TABLE ([ID] int, [Name] [nvarchar] (500))
DECLARE @name NVARCHAR(255)
DECLARE @pos INT
DECLARE @index INT
DECLARE @return_value varchar(max)
SET @index = 0
SET @return_value = null

WHILE CHARINDEX(@delimiter, @stringToSplit) > 0
BEGIN
SELECT @pos  = CHARINDEX(@delimiter, @stringToSplit)  
SELECT @name = SUBSTRING(@stringToSplit, 1, @pos-1)

INSERT INTO @returnList 
SELECT @index, @name

SELECT @stringToSplit = SUBSTRING(@stringToSplit, @pos+1, LEN(@stringToSplit)-@pos)
SET @index = @index + 1 END

INSERT INTO @returnList
SELECT @index, @stringToSplit

SELECT @return_value = Name from @returnList where [ID] = @indexToReturn

RETURN @return_value
END
GO

```