• The article presents a nice example of a possible use for a new technique - but unfortunately, the choice of examples is a bit bad.

    I think every advanced SQL Server developer should make sure to know what's in his/her toolkit. And that involves a thorough knowledge of all builtin functions. All too often, that knowledge lacks, and people invent wheels that are already built into the product.

    Example 1 ("occurs") to count the number of characters 't' in the string:

    SELECT LEN(@Str) - LEN(REPLACE(@Str, 't', '')

    Added advantage - can also cound substrings of longer length:

    SELECT (LEN(@Str) - LEN(REPLACE(@Str, 'test', '')) / LEN('test')

    Example 2: ("phone_clean") to remove non-numeric characters:

    At first glance a good example. Though I agree that using a CLR function to leverage regex functionality is better (not primarly for speed, but because regex functionality is existing, tried-and-tested). Unless you have a DBA who forbids CLR functions, of course.

    On second look - the string concatenation technique used to piece the bits back together is undocumented, and might cause unexpected results. Not good for production code.

    Example 3: ("replace_Nth_char") to replace a specific character in the middle of the string:

    SELECT STUFF(@Str1,3,1,'0')

    Added advantages: (1) avoids undocumented string concatenation (see above), and (2) both replaced and replacement string can be of any length

    SELECT STUFF(@Str1, 12, 20, '')

    SELECT STUFF(@Str1, 10, 0, 'Inserted text')


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/