Skip to content
Advertisement

How to use RegEx in the SQL function CHARINDEX to find the nth occurrence

The intent of the following algorithm is to extract certain set of fields from a key (in this example, extract first 2 fields), and it works. The fields are delimited by a colon:

Result: Field-1:Field-2

Microsoft document says that the first parameter is an expression, but I think what they mean by that in the context of CHARINDEX is, that this expression should evaluate to a string literal; hence the following attempt to pass a RegEx to get the 2nd occurrence doesn’t work; obviously either it is not supported or I am using a bad syntax:

In other words, is it possible to find the position of the nth occurrence of the delimiter in a given text using RegEx, so that I could avoid several nested CHARINDEX or a loop to parse? Keeping aside, if n is passed as a parameter, then I can’t even use static nesting anymore…

Thanks in advance for the help.

Environment: Microsoft SQL Server 2014 (SP3) Standard Edition (64-bit)

Advertisement

Answer

There is no easy way, only tricks, to extract nth substring of a string. Below is a set based, recursive CTE approach:

If loop is an option then:

DB<>Fiddle

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement