If
you need to look for a specific word or substring within a string, you can
achieve it with the help of SQL Server CHARINDEX function
CHARINDEX
function is used to search for specific word or substring in overall string and
returns its starting position of match. If no matching word found then it will
return 0.
CHARINDEX
syntax:
CHARINDEX (expressionToFind, expressionToSearch
[, start_location ] )
|
Example
to find specific word:
DECLARE @testStr
VARCHAR(250)
SET @testStr='This is a test string'
SELECT CHARINDEX('test', @testStr)
Output:
-----------
11
(1 row(s)
affected)
|
Example
to find word with specific start location defined:
DECLARE @testStr VARCHAR(250)
SET @testStr = 'This is a test string'
SELECT CHARINDEX('test', @testStr, 5)
Output:
-----------
11
(1 row(s) affected)
|
Comments
Post a Comment