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
jknj72Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
The low-tech approach would be to get the length of the string, then get the length of the string with all commas removed, then add one.
declare @str nvarchar(100) = 'E,F,G ,3,IK'   
SELECT len(@str) - len(replace(@str,',','')) + 1

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SharathData EngineerCommented:
You can try like this.

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

select len(@stringlist)-len(replace(@stringlist,',',''))+1
0
virtuadeptCommented:
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.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

virtuadeptCommented:
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.
0
jknj72Author Commented:
thanks jimhorn
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks for the grade.  Good luck with your project.  -Jim
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.