Skip to content
Advertisement

Use PATINDEX to extract a substring in SQL Server?

I have some specific values I want to extract out of a string in SQL Server, but I’m not sure exactly how to get it done with PATINDEX.

Take this string:

I want to extract out the values of 500000 (for @RowsPerRun), 10000 for @RowsPerBatch and the value of 7 for @NbrDaysToKeepRpt. The values will be of variable length, so I can’t guarantee the @RowsPerRun value will be 6 characters.

Is that possible?

Advertisement

Answer

Returns:

Note that I included a few extra columns to help you understand what’s happening.

Update: Against a table

This is how you would apply this logic to a series of values:

Returns:

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