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 Check if String Contains Specific Word / String with CHARINDEX Function

Feb 25, 2013
Introduction

Here I will explain how to check if string contains specific word in SQL Server using CHARINDEX function or SQL Server check if string contains specific substring with CHARINDEX function. 

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 how to check if string contains specific word in SQL Server

To check if string contains specific word in SQL Server we can use CHARINDEX function.

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 CHARINDEX

CHARINDEX ( StringToFind ,OverAllStringToSearch [ , start_location ] )
Example 1


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


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

2 comments :

Anonymous said...

why you write trivial articles?
you want to win the presence in idiotic google and create more garbage to it?

Anonymous said...

So the second query gives same result as first query????????????

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.