Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
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 the Nth Occurrence of a Character in a String

By Cade Bryant,

T-SQL's CHARINDEX() function is a useful for parsing out characters within a string.  However, it only returns the first occurrence of a character.  Oftentimes one needs to locate the Nth instance of a character or a space, which can be a complicated task in standard T-SQL.  This function I wrote, imaginatively named CHARINDEX2(), allows one to do this.  It receives, as arguments, the target string or character, the string to be searched, and the occurrence of the target string/character.

Total article views: 4359 | Views in the last 30 days: 19
 
Related Articles
SCRIPT

Trim Non-Alpha characters from string

Trim Non-Alpha characters from string

FORUM

Random 64 Characters alphanumeric String

Random 64 Characters alphanumeric String

SCRIPT

Extarcting string after and before a Character/Pattern

Finding characters/string before and after some character or pattern

SCRIPT

ASCII Character Groups in String

Powerful function to find strings containing or excluding classes of ASCII characters.

FORUM

Conversion failed when converting datetime from character string

Conversion failed when converting datetime from character string

Tags
miscellaneous    
t-sql    
 
Contribute