AspBucket offers ASP.NET, C#, VB, Jquery, CSS, Ajax, SQL tutorials. It is the best place for programmers to learn

Tuesday, 19 January 2016

Use of Charindex Function in SQL with an example

In this article We will discuss How to use Charindex Function in SQL Server. It is utilized to hunt down particular keywords or sub-string in string and gives back to its beginning position of the match string. In the event that if no word found, then it will return 0.


Syntax of Charindex function

CHARINDEX ( SearchString ,OverAllString , [start_location ] )

  • SearchString: Replace SearchString with  search text
  • OverAllString: Replace OverAllString with given full text.
Example:
Read following example carefully for charindex function

DECLARE @str NVARCHAR(MAX)
SET @str='Learn how to use Charindex in aspbucket.com'
SELECT CHARINDEX('aspbucket',@str)
Output of above query is 31


DECLARE @str NVARCHAR(MAX)
SET @str='Learn how to use Charindex in aspbucket.com'
SELECT CHARINDEX('aspbucket',@str,8)
Above query search will start from 8th position of string and we will get output like as shown below
Output of above query is 31

0 comments :

Post a comment

  • Popular Posts
  • Comments