A nugget!

Published 08 December 09 08:56 PM | MartinBell
Here is something I thought was very nugget worthy!

Q: If you have a delimited string how can you easily find the number of elements without splitting the string?

A: This is one way!

Example:

A comma separated string with 4 elements -

DECLARE @str varchar(max) = 'F7C52CB0,980CADCC,1C4195F2,7881934F'

SELECT LEN(@str), LEN(REPLACE(@str,',','')), LEN(@str) - LEN(REPLACE(@str,',',''))+1

 

Filed under:

Comments

No Comments