CHARINDEX IN SQL

database, blue, sql-2394312.jpg

The SQL has many built-in function that helps SQL Developer to achieve their goal or finish their query with expected result.

The CHARINDEX is one of the built-in function which is powerful and useful. This Function gets the position of a substring of string.

Below is the syntax for the CHARINDEX :

CHARINDEX(<SUBSTRING>,<STRING>,<POSITION>) SUBSTRING – The value that user needs to find its position in the given STRING POSTION – The position from where its start looking, by default its value is 1

Here is an example for CHARINDEX

Here the POSITION of ‘S’ in ‘SQLBEAST’ is 9.

Even with the third parameter passes as 1 still gives some result, Thus by default the POSITION parameter is always 1.

Now if you see we have two S in string SQLBEAST, so the CHARINDEX always return the first encounter position. To prove this we will change the third parameter value to 2, which will instruct the CHARINDEX to start looking for substring ‘S’ from second position that is from ‘Q’. Which will ignore the first ‘S’ position and return 7.

And at last the function is case insensitive, we will still get same answer even with smallcase ‘s’.

Below is the video where CHARINDEX IS explained in a easy way.

1 thought on “CHARINDEX IN SQL”

Comments are closed.