Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Find Nth Occurrence of Character Function V3

By Piquet,

In the tradition of best practice & code improvement, a third iteration of a script that finds the Nth occurrence of a Target string within another string (Original author unknown, V2 author vextant).

This version returns zero if an Nth occurrence is not found (similar to V1), but also provides the ability to search the string Right to Left so that you can find the last occurrence, 2nd to last occurrence, etc.
The code has also been simplified.

(It's international Talk Like a Pirate Day today)

Total article views: 403 | Views in the last 30 days: 1
Related Articles

SQL BLOG: First Occurrence Of Character In a String

Scenario:  We need to find the first occurrence of character ie non numeric data in the string ...


Find the position of all occurrences of an expression within a string

This iTVF returns both the relative and the actual positions of ALL occurrences of a string within a...


String Parsing

Find the second occurrence of \ in a string and replace everything after with blank space


Search for string occurrence

Can this be done? Does anyone know of a tool or command I could use that can find all occurrences o...


Search a string in a text with number of occurrences

IF you have a long string and if you want to search a specific input string, and number of timeS the...


Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones