SQL Porosity - CSV Element Calculation

Background:
Recently "walked" into a scenario where a client was storing an variable sized CSV string in a field.  This field held data that would eventually be evaluated as a "pseudo" entity for reporting purposes.

Problem
Due to the need for performance and reliability, the normal practice of splitting the string into a result set and then performing a SQL Count on the result set, would be overly complex and may cause performance issues as the length is variable (i.e. varchar(max) )

Solution
After reviewing several search queries, which most ended up at my old faithful Stack Overflow.  I came across an approach that is nothing more than Porosity.  For my purpose, it fit the bill very cleanly without overly transitioning a single value into a data-set and then calculating the data-set.

Net Result
Data-Set:
Source:
SQL Porosity - Source Array
SQL Data - Source Value
SQL Porosity - Resulting Array
SQL Data - Comma's Voided
Math
n - Len([Field])
m - Len(Replace([Field], ',', ''))
Number of Elements: n - m + 1

Explanation:
First we need to know the source length (with comma's); next, we remove the comma's with empty space; next, we find the length of the string without the comma's; lastly, we subtract the "voided" length from the "source" length, to find the number of comma's that were in the string.  Then we add 1 to the difference to give visibility to the number of value elements within the CSV value.

Comments

Popular posts from this blog

SysInternals - BgInfo for ALL Users

JSON/AJAX Helpers

Linked Server....