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.
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
Check following SQL statement for charindex function syntax
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
---------------------------------------
(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
---------------------------------------
(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. |
|||
|
|||
1 comments :
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.
Note: Only a member of this blog may post a comment.