ExampleGet your own SQL Server Search for "t" in string "Customer", and return position: SELECTCHARINDEX('t','Customer')ASMatchPosition; Try it Yourself » Definition and Usage The CHARINDEX() function searches for a substring in a string, and returns the position. ...
This example returns the first location of the string is in string This is a string, starting the search from position 4 (the fourth character).SQL Afrita SELECT CHARINDEX('is', 'This is a string', 4); Here is the result set.
This example returns the first location of the string is in string This is a string, starting from position 1 (the first character) of This is a string. SQL Copy SELECT CHARINDEX('is', 'This is a string'); Here is the result set. Copy --- 3 G. Searching from a position ot...
This example shows the return value when CHARINDEX does not find stringstring_patternin the searched string. SQL SELECTTOP(1)CHARINDEX('at','This is a string')FROMdbo.DimCustomer; Here's the result set. --- 0 See also LEN (Transact...
This example shows the return value when CHARINDEX does not find stringstring_patternin the searched string. SQL SELECTTOP(1)CHARINDEX('at','This is a string')FROMdbo.DimCustomer; Here's the result set. --- 0 See also LEN (Transact...
This example returns the first location of the string is in string This is a string, starting from position 1 (the first character) of This is a string. SQL Copy SELECT CHARINDEX('is', 'This is a string'); Here's the result set. Copy --- 3 G. Searching from a position oth...
The following are the SQL Server PATINDEX() function examples. Example 1: Use PATINDEX in a literal string input Let’s consider a simple example of PATINDEX. For example, I want to find the position of the word “example” in the “Wild-card example” input string. To do that, execu...
The CHARINDEX function finds the position of the expression in another expression. Syntax: select charindex(Exptofind ,Exptosearch ,[Start_Position]) Example 1: select charindex('a','Rakesh') [Position] -- Here 'a' position is 2 select charindex('H','Hello World') [Position] -- ...
Example 5: SQL CHARINDEX with SQL CASE statement We can use SQL CHARINDEX with SQL Case statement to search a particular substring existence in a specified string. 1 2 3 4 5 6 7 8 9 10 11 DECLARE @Name AS VARCHAR(100)= 'Explore SQL Server with articles on SQLShack'; SELECT CASE...
Example Let's look at some SQL Server CHARINDEX function examples and explore how to use the CHARINDEX function in SQL Server (Transact-SQL). For example: SELECT CHARINDEX('t', 'TechOnTheNet.com');Result:1(search is not case-sensitive so it will match on 'T')SELECT CHARINDEX('t', '...