HI WELCOME TO KANSIRIS

SQL Server Charindex Function Example in 2008

Leave a Comment
CHARINDEX Function

This function is used to search for specific word or substring in overall string and returns its starting position of match. In case if no word found then it will return 0 (zero).

Syntax of SQL CHARINDEX function

Check following SQL statement for charindex function syntax


CHARINDEX ( StringToFind ,OverAllStringToSearch [ , start_location ] )

Example 1

Check following example for charindex function SQL Server


DECLARE @str VARCHAR(250)
SET @str='Welcome to kansiris
SELECT CHARINDEX('Aspdotnet',@str)


Output

Once we run above query we will get output like as shown below

---------------------------------------
12
(1 row(s) affected)
Example 2

Another example for charindex function SQL Server

DECLARE @str VARCHAR(250)
SET @str='Welcome to kansiris
SELECT CHARINDEX('Aspdotnet',@str,7)

Output

Once we run above query search will start from 7th position of string and we will get output like as shown below

---------------------------------------
12
(1 row(s) affected)

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.