Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

SQL Server Charindex Function Example in 2008

Nov 20, 2015
Introduction

Here I will explain what is charindex function in SQL Server and uses of charindex function in SQL Server. Generally charindex function in SQL Server is used to search for specific word or substring in overall string and returns starting position of match. Generally we will use charindex in SQL Server to check if string contains specific word or not.

Description:
  
In previous articles I explained SQL Query to get particular part of string, SQL Query to replace part of string, kill all active database connections in SQL Server, SQL Query to read xml file, SQL Query to get month wise, year wise data and many articles relating to SQL Server,
jQuery, JavaScript. Now I will explain charindex function in SQL Server with example

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 Aspdotnet-Suresh.com'
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 Aspdotnet-Suresh.com'
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)


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

1 comments :

Mlm Consultancy said...


Thanks for sharing this article. This is very Help full to me. You really write very well. I am great fan of your blog, keep writing.
Thanks for sharing sir.

Give your Valuable Comments

Other Related Posts

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.