Link to home
Start Free TrialLog in
Avatar of jknj72
jknj72

asked on

SQL Server function parse

I need to create a function FN_getstrListCount to return the number of items in the variable @stringlist separated by comma.                                                
                                                
declare @stringlist nvarchar(100)                                                
set @stringlist = 'E,F,G ,3,IK'                                                
Any help would be appreciated

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can try like this.

declare @stringlist nvarchar(100)                                                
set @stringlist = 'E,F,G ,3,IK'  

select len(@stringlist)-len(replace(@stringlist,',',''))+1
Well as long as you know comma is not going to be the last character of the string, then you know that the count of things between commas will be number of commas + 1.

SELECT @comma_count = LEN(@stringlist) - LEN(REPLACE(@stringlist,',',''))

What that does is compare lenght of the string versus length of the string without the commas so you know how many characters in the string are commas.
If you need to evaluate the actual values between the commas, I have a function that will return a table of the values between specificed delimeter.
Avatar of jknj72
jknj72

ASKER

thanks jimhorn
Thanks for the grade.  Good luck with your project.  -Jim