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)

Extend CHARINDEX with occurance matching

By Partha Pal,

Often in our day to day sql query activities we require to retrieve the CHARINDEX from string value which we easily can achieve using tsql CHARINDEX . Requirements often extends to the scenario that we need to consider 1st occurance only or 2nd occurance only where we know the searched string may appear more than once in the value string. But in that case usage of CHARINDEX becomes bit complecated as we need to do lot of string manipulation to get the desired charindex position.


To overcome this limitation, I tried to extend the functionality of CHARINDEX with a new user defined function. This udf is actually based on 2 mostly used string functon CHARINDEX & SUBSTRING.

If we look at CHARINDEX signature it's CHARINDEX ( expression1 ,expression2 [ , start_location ] ). If we try to find 'A' from a string 'ABDABRCTAB' using CHARINDEX we need to call it as :

a) for the 1st occurance


b) for the 2nd occurance


c) for the 3rd occurance


Actually we need to know before hand the where from we should start (ie start location)


But using my proposed udf (I've named it as ufnGetCharIndexWithOccurance) we need to call it as

ufnGetCharIndexWithOccurance ( expression1 ,expression2, intended_occurance)

where we need to search string <expression1> in string <expression2> for the occurance integer type of <intended_occurance>.

So if we reconsider the example explained above using this new udf the usage will be

a) for the 1st occurance

ufnGetCharIndexWithOccurance('AB', 'ABDABRCTAB', null) or ufnGetCharIndexWithOccurance('AB', 'ABDABRCTAB', 0) or ufnGetCharIndexWithOccurance('AB', 'ABDABRCTAB', 1)

b) for the 2nd occurance


c) for the 3rd occurance


and so on.

Now this udf has been created keeping in mind, it will use the server Collation on which the function will be executed.


Sounds good !! So what's there to wait ... let's try using it and get rid off repeated usage of same code block !


Cheers !!

Total article views: 1397 | Views in the last 30 days: 2
Related Articles

String Manipulation - CHARINDEX()

I see charindex used quite commonly in string manipulation.  What I rarely see used is the optional ...


Extend CHARINDEX with occurance matching

Comments posted to this topic are about the item [B]Extend CHARINDEX with occurance matching[/B] Thi...


Find last occurence of a character within a string

Find last occurence of a character within a string


Function for String Occurences Count

This function is used to get the particular string occurences count from the given input string.


Tame Those Strings! Part 2 - Using CHARINDEX

The second part of Steve Jones's series on programming and manipulating strings in T-SQL.


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