Table of Contents
- RIGHT and LEFT
- CHARINDEX
- SUBSTRING
- Using them together
- References
- See Also
This article explains the functionality and uses of the LEFT, RIGHT, SUBSTRING and CHARINDEX functions in SQL.
This article will leave you with sound knowledge and understanding that you can take away and questions will be asked no more.
We”ll start by explaining each function individually with examples; and then I will show a scenario where these functions can be used together.
RIGHT and LEFT
These functions might sound fairly self explanatory, but unless you have a fundamental understanding of how they behave; you will experience some problems that will keep you scratching your head for a while.
As you can see by this illustration, the LEFT function starts BEFORE the left-most character of a string and moves to the right, while the RIGHT function starts AFTER the right-most character and moves inwards to the left.
SELECT RIGHT('HELLO WORLD', 3);
SELECT LEFT('HELLO WORLD', 3);
Here’s the result:
As you can see, the RIGHT function has expectedly taken the last three characters of the string passed into it, and the LEFT function has taken the first three. Pretty simple!
CHARINDEX
CHARINDEX is another simple function that accepts two arguments. The first argument is the character you are searching for; the second is the string. It will return the first index position that the character passed into the first argument is within the
string.
Now let’s use our CHARINDEX function to find the position of the space in this string:
SELECT CHARINDEX(' ','Hello World');
Here’s the result:
As you can see, the position of the space within “Hello World” is the 6th character. CHARINDEX can be a useful function for finding occurrences of a character within a table programmatically. I will build on this subject later on in this article.
SUBSTRING
I would consider SUBSTRING to be the most useful of all the functions mentioned today. It accepts three arguments, the string, a start position and how many characters it will “step over”. Let’s take a look at that illustration from earlier:
Now I’ll write a simple query to show the use of SUBSTRING:
SELECT SUBSTRING('HELLO WORLD',4,5)
And now the results:
As you can see. SUBSTRING includes spaces as a position within a string. So executing this query shows a “window” of the string that has been passed to it. If we had executed the query as “SELECT SUBSTRING(‘HELLO WORLD’,6,5)” then the results would have
shown ” WORL”.
Using them together
Now I’m going to show an example of how to use these together. Imagine you have a table with a column called “Name”, within that column you have various names, with different lengths; but all have one thing in common, a space. You’re asked to only display
the forename, but because there are differing lengths you will need to find the occurring space in the string.
SELECT CHARINDEX(' ','JOHNNY BELL')
We can use the CHARINDEX function to find the position of the space within the row programmatically. In this instance, the position is “7”. Now we’ve found that we just need to display everything left of that position. We can “wrap” this up within a LEFT
statement to do this (simple right?!).
SELECT LEFT('HELLO WORLD',CHARINDEX(' ','HELLO WORLD')-1)
Notice how I’ve put a “-1” after the CHARINDEX function? This is because the CHARINDEX function is finding the space in the string, but we don’t really want to include this in our resultset, so we’re basically saying “find the position of the space minus
one”. A good way to see this is by looking at the illustration from earlier and count the positions that the function will step over. Remember that the LEFT function takes two arguments, so we need to state the expression within that as well. This will of
course return the result “JOHNNY”.
We hope this helps, thanks for reading and if you have any further questions then don’t hesitate to comment below.
This entry participates in the TechNet Guru contributions for June, 2013 contest.
References
- String Functions (Transact-SQL)
- CHARINDEX (Transact-SQL)
See Also
- T-SQL: Split String with a Twist
- Transact-SQL Portal
При работе с базой данных SQL вам может понадобиться найти записи, содержащие определенные строки. В этой статье мы разберем, как искать строки и подстроки в MySQL и SQL Server.
Содержание
- Использование операторов WHERE и LIKE для поиска подстроки
- Поиск подстроки в SQL Server с помощью функции CHARINDEX
- Поиск подстроки в SQL Server с помощью функции PATINDEX
- MySQL-запрос для поиска подстроки с применением функции SUBSTRING_INDEX()
Я буду использовать таблицу products_data
в базе данных products_schema
. Выполнение команды SELECT * FROM products_data
покажет мне все записи в таблице:
Поскольку я также буду показывать поиск подстроки в SQL Server, у меня есть таблица products_data
в базе данных products
:
Поиск подстроки при помощи операторов WHERE и LIKE
Оператор WHERE позволяет получить только те записи, которые удовлетворяют определенному условию. А оператор LIKE позволяет найти определенный шаблон в столбце. Эти два оператора можно комбинировать для поиска строки или подстроки.
Например, объединив WHERE с LIKE, я смог получить все товары, в которых есть слово «computer»:
SELECT * FROM products_data WHERE product_name LIKE '%computer%'
Знаки процента слева и справа от «computer» указывают искать слово «computer» в конце, середине или начале строки.
Если поставить знак процента в начале подстроки, по которой вы ищете, это будет указанием найти такую подстроку, стоящую в конце строки. Например, выполнив следующий запрос, я получил все продукты, которые заканчиваются на «er»:
SELECT * FROM products_data WHERE product_name LIKE '%er'
А если написать знак процента после искомой подстроки, это будет означать, что нужно найти такую подстроку, стоящую в начале строки. Например, я смог получить продукт, начинающийся на «lap», выполнив следующий запрос:
SELECT * FROM products_data WHERE product_name LIKE 'lap%'
Этот метод также отлично работает в SQL Server:
Поиск подстроки в SQL Server с помощью функции CHARINDEX
CHARINDEX() — это функция SQL Server для поиска индекса подстроки в строке.
Функция CHARINDEX() принимает 3 аргумента: подстроку, строку и стартовую позицию для поиска. Синтаксис выглядит следующим образом:
CHARINDEX(substring, string, start_position)
Если функция находит совпадение, она возвращает индекс, по которому найдено совпадение, а если совпадение не найдено, возвращает 0. В отличие от многих других языков, отсчет в SQL начинается с единицы.
Пример:
SELECT CHARINDEX('free', 'free is the watchword of freeCodeCamp') position;
Как видите, слово «free» было найдено на позиции 1. Это потому, что на позиции 1 стоит его первая буква — «f»:
Можно задать поиск с конкретной позиции. Например, если указать в качестве позиции 25, SQL Server найдет совпадение, начиная с текста «freeCodeCamp»:
SELECT CHARINDEX('free', 'free is the watchword of freeCodeCamp', 25);
При помощи CHARINDEX можно найти все продукты, в которых есть слово «computer», выполнив этот запрос:
SELECT * FROM products_data WHERE CHARINDEX('computer', product_name, 0) > 0
Этот запрос диктует следующее: «Начиная с индекса 0 и до тех пор, пока их больше 0, ищи все продукты, названия которых содержат слово «computer», в столбце product_name». Вот результат:
Поиск подстроки в SQL Server с помощью функции PATINDEX
PATINDEX означает «pattern index», т. е. «индекс шаблона». Эта функция позволяет искать подстроку с помощью регулярных выражений.
PATINDEX принимает два аргумента: шаблон и строку. Синтаксис выглядит следующим образом:
PATINDEX(pattern, string)
Если PATINDEX находит совпадение, он возвращает позицию этого совпадения. Если совпадение не найдено, возвращается 0. Вот пример:
SELECT PATINDEX('%ava%', 'JavaScript is a Jack of all trades');
Чтобы применить PATINDEX к таблице, я выполнил следующий запрос:
SELECT product_name, PATINDEX('%ann%', product_name) position FROM products_data
Но он только перечислил все товары и вернул индекс, под которым нашел совпадение:
Как видите, подстрока «ann» нашлась под индексом 3 продукта Scanner. Но скорее всего вы захотите, чтобы выводился только тот товар, в котором было найдено совпадение с шаблоном.
Чтобы обеспечить такое поведение, можно использовать операторы WHERE и LIKE:
SELECT product_name, PATINDEX('%ann%', product_name) position FROM products_data WHERE product_name LIKE '%ann%'
Теперь запрос возвращает то, что нужно.
MySQL-запрос для поиска строки с применением функции SUBSTRING_INDEX()
Помимо решений, которые я уже показал, MySQL имеет встроенную функцию SUBSTRING_INDEX(), с помощью которой можно найти часть строки.
Функция SUBSTRING_INDEX() принимает 3 обязательных аргумента: строку, разделитель и число. Числом обозначается количество вхождений разделителя.
Если вы укажете обязательные аргументы, функция SUBSTRING_INDEX() вернет подстроку до n-го разделителя, где n — указанное число вхождений разделителя. Вот пример:
SELECT SUBSTRING_INDEX("Learn on freeCodeCamp with me", "with", 1);
В этом запросе «Learn on freeCodeCamp with me» — это строка, «with» — разделитель, а 1 — количество вхождений разделителя. В этом случае запрос выдаст вам «Learn on freeCodeCamp»:
Количество вхождений разделителя может быть как положительным, так и отрицательным. Если это отрицательное число, то вы получите часть строки после указанного числа разделителей. Вот пример:
SELECT SUBSTRING_INDEX("Learn on freeCodeCamp with me", "with", -1);
От редакции Techrocks: также предлагаем почитать «Индексы и оптимизация MySQL-запросов».
Заключение
Из этой статьи вы узнали, как найти подстроку в строке в SQL, используя MySQL и SQL Server.
CHARINDEX() и PATINDEX() — это функции, с помощью которых можно найти подстроку в строке в SQL Server. Функция PATINDEX() является более мощной, так как позволяет использовать регулярные выражения.
Поскольку в MySQL нет CHARINDEX() и PATINDEX(), в первом примере мы рассмотрели, как найти подстроку в строке с помощью операторов WHERE и LIKE.
Перевод статьи «SQL Where Contains String – Substring Query Example».
SQL Charindex(), Locate(), Instr() Functions
The SQL CHARINDEX() | LOCATE() | INSTR() is a function and returns the index position of the first occurrence of substring of a given input string or text.
The SQL CHARINDEX() use to find the numeric starting position of a search string inside another string.
The SQL CHARINDEX() function returns “0” if given substring does not exist in the input string.
The SQL CHARINDEX() function is supports or work with character and numeric based columns.
It can be used in any valid SQL SELECT statement as well in SQL where clause.
Sql charindex, locate, instr using charindex sql server example, sql substring charindex, find last occurence, find nth occurence, get index position,
get character position, search from right.
The basic syntax to retrieve index posiotion of a substring from a given input string
For Sql Server
SELECT CHARINDEX(sub_string1, string1[, start_location]);
For MySql
SELECT LOCATE(sub_string1, string1[, start_location]);
In the above both syntax has same arguments in their function and below detail of that arguments.
Parameter EmpName | Description |
---|---|
sub_string1 | Required. The string to find the index position of a sequence of characters. |
string1 | Required. The sequence of characters or string that will be searched for to index position of substring1 in string1 or column_EmpName1. |
start_location | Optional. Instructs the function to ignore a given number of characters at the beginning of the string to be searched. |
For MS Access
SELECT INSTR([start_location,] string1, sub_string1);
Here, parameters are in reverse order, but meaning is same.
SQL CHARINDEX() | LOCATE() | INSTR() Example – Using Expression Or Formula
The following SQL SELECT statement find the index position of sequence of characters or a string within a string.
For SQL Server
SELECT
CHARINDEX('a', 'Sql Database') AS 'Find Index Of a',
CHARINDEX('a', 'Sql Database', 7) AS 'Skip 7 characters and Find Index Of a';
For MySql
SELECT
LOCATE('a', 'Sql Database') AS 'Find Index Of a',
LOCATE('a', 'Sql Database', 7) AS 'Skip 7 characters and Find Index Of a';
For MS Access
SELECT
INSTR('Sql Database', 'a') AS 'Find Index Of a',
INSTR(7, 'Sql Database', 'a') AS 'Skip 7 characters and Find Index Of a';
The result of above query is:
Find Index Of ‘a’ | Skip 7 characters and Find Index Of ‘a’ |
---|---|
6 | 8 |
Sample Database Table – Employee
ID | EmpEmpName | Designation | Dept | JoinYear | Salary |
---|---|---|---|---|---|
1 | Harish Karthik | Manager | MS Access | 2012 | 7040 |
2 | Devi Mai | Mobile Database | ASP.Net | 2012 | 20480 |
3 | Hanumanthan | Computer Science | MySQL | 2012 | 12290.3 |
4 | Sakunthala | Cloud Database | PHP | 2015 | 2000 |
5 | Keshavan | Database Security | MS Access | 2012 | 19640 |
SQL CHARINDEX() | LOCATE() | INSTR() Example – With Table Column
The following SQL statement CHARINDEX the “EmpName” and “Designation” column from the “Employee” table:
For SQL SERVER
SELECT
EmpName, CHARINDEX('i', EmpName) As 'Index Of i in EmpName',
Designation, CHARINDEX('data', Designation) As 'Index Position Of data in Designation'
FROM Employee;
For MySql
SELECT
EmpName, LOCATE('i', EmpName) As 'Index Of i in EmpName',
Designation, LOCATE('data', Designation) As 'Index Position Of data in Designation'
FROM Employee;
For MS Access
SELECT
EmpName, INSTR('i', EmpName) As 'Index Of i in EmpName',
Designation, INSTR('data', Designation) As 'Index Position Of data in Designation'
FROM Employee;
The result of above query is:
EmpName | Index Of “i” in EmpName | Designation | Index Of “data” in Designation |
---|---|---|---|
Harish Karthik | 4 | Manager | 0 |
Devi Mai | 4 | Mobile Database | 8 |
Hanumanthan | 0 | Computer Science | 0 |
Sakunthala | 0 | Cloud Database | 7 |
Keshavan | 0 | Database Security | 1 |
Sql server charindex, instr, locate using substring index, sql server indexof, sql patindex example, find string position, find index in sql server,
pattern matching, case insensitive, search position from right.
SQL Charindex Case
CASE statement with CHARINDEX. We can check whether a string or a character is present in another string. The character or word is present if the CHARINDEX function returns a non-zero index number. It wouldn’t exist if it didn’t.
Example 1: Here is the Example.
DECLARE @Name as VARCHAR(100) = 'Sha Ka Ba';
SELECT CASE WHEN CHARINDEX('Ka', @Name) > 0 THEN 'Exists'
ELSE 'Not Exists'
END as Test;
SELECT CASE WHEN CHARINDEX('Pk', @Name) > 0 THEN 'Exists'
ELSE 'Not Exists'
END as Test;
Example 2: When clauses only need to have these three requirements. To ensure that the > character is present in the string, use charindex:
CASE
WHEN commodity IS NULL THEN 'No Comodity'
WHEN CHARINDEX('>', Commodity) > 0 THEN
SUBSTRING(commodity, CHARINDEX('>', commodity) + 2, LEN(commodity))
ELSE comodity
END
SQL Charindex Last Occurence
CHARINDEX makes it simple to find the first instance of a string in another string. It can also provide a simple technique of determining the final occurrence of a string by reversing the order of the string.
To identify the position of the last ‘/’ character, CHARINDEX searches the reversed string. All characters to the right of this position are subsequently extracted using the RIGHT function.
For instance if a string contains the full path to a file, such as :
C:Program FilesMicrosoft SQL ServerMSSQLDATAAdventureWorks_Data.mdf
This technique can be used to extract just the filename (i.e. the part after the last ”) :
AdventureWorks_Data.mdf
I have column called assocname.
Sample data:
- FirstParty>Individual:2
- FirstParty:3>FPRep:2>Individual
- Incident>Vehicle:2>RegisteredOwner>Individual3
I want to get the string before the last occurrence ‘>’. Here is the result:
- FirstParty
- FirstParty:3>FPRep:2
- Incident>Vehicle:2>RegisteredOwner
Example 1: Using the RIGHT function, we can easily strip off just the filename by looking for the last occurrence of the delimiter “. That’s exactly what the SQL below accomplishes:
DECLARE @FullPath VARCHAR(200)
SET @FullPath =
'C:Program FilesMicrosoft SQL ServerMSSQLDATAAdventureWorks_Data.mdf'
SELECT RIGHT(@FullPath , CHARINDEX ('' ,REVERSE(@FullPath))-1)
If we run this query it returns just the filename.
Example 2: The following query uses this technique to extract the file name from the full path name in the sys.master_files system view :
SELECT physical_name
,RIGHT(physical_name,CHARINDEX('',REVERSE(physical_name))-1) file_name
FROM sys.master_files
Example 3: In the example given below, we need to search for the last occurrence of word ‘the’ in the sentence.
DECLARE @String AS VARCHAR(100)
DECLARE @Search_String AS VARCHAR(100)
SET @String ='The SQL SERVER is one of the best applications of Microsoft'
SET @Search_String='the'
Example 4: Find Last occurrence of any character/word in the string:
SELECT DATALENGTH(@String)-CHARINDEX(REVERSE(@Search_String)
,REVERSE(@String))-1 As [Last occurrence]
Output:
SQL Charindex Pattern Matching
Charindex vs Patternindex
The CHARINDEX and PATINDEX functions are used to determine a pattern’s starting location. Another distinction is that with CHARINDEX, the pattern to be searched is limited to 8000 bytes.
Similarity:
- Both routines take two arguments and search for the required text in the given expression.
- The initial location of the matching pattern specified in the function is returned by both functions.
Difference:
- With wildcard characters, the PatIndex function is utilised. The wildcard characters must be enclosed before (when searching last) or after (when searching first) the searching text.
- With the supplied searching pattern, the CharIndex function cannot utilise any wildcard characters. The wildcard characters will not be recognised by the CharIndex function.
- PATINDEX() allows you to search for patterns using wildcard characters. CHARINDEX(), on the other hand, does not.
- The third input to CHARINDEX() allows you to select the search’s start location. PATINDEX(), on the other hand, does not.
- CHARINDEX() looks for one character expression within a second character expression and returns the first expression’s starting position if it is found.
- PATINDEX() Returns the starting position of the first occurrence of a pattern in a specified expression, or zeros if the pattern is not found, on all valid text and character data types.
Syntax:
And here’s the official syntax of each function.
CHARINDEX()
CHARINDEX ( expressionToFind , expressionToSearch [ , start_location ] )
PATINDEX()
PATINDEX ( '%pattern%' , expression )
Both functions search for a character expression, however CHARINDEX() takes three arguments whereas PATINDEX() takes only two. The optional third input to the CHARINDEX() function allows you to set the search’s beginning location. In other words, you can use the CHARINDEX() function to only return matches that occur after a specified point in the string.
Example 1: Example of PatIndex
SELECT (PATINDEX('%Corner%', 'C-SharpCorner'))
Result:
8
Example of CharIndex
SELECT (charindex('Corner', 'C-SharpCorner'))
Result:
8
Example 2: Here are examples that demonstrate the extra functionality provided by each function.
CHARINDEX()
Here, I add a third input to have a starting point for the search. In this example, it will skip the first occurrence of Bob and return the second occurrence’s place.
SELECT CHARINDEX('Bob', 'Bob likes beer. Bob also likes beef.', 16);
Result:
17
So as mentioned, you can’t do this with PATINDEX() because it doesn’t accept that third parameter.
PATINDEX()
Now consider what you can accomplish with PATINDEX() that you can’t do with CHARINDEX() (). In this case, we’re looking for a pattern using wildcard characters:
SELECT PATINDEX('%b_er%', 'Bob likes beer.');
Result:
11
In this situation, we’re using percent signs (%) as wildcards to indicate that any number of characters could come before or after our search phrase. We also employ the underscore (_), which is a wildcard character that can be used to represent any single character.
Conclusion: While both CHARINDEX() and PATINDEX() perform comparable tasks and may be used interchangeably in many situations, there are circumstances when you’ll need to utilise one over the other.
Performance: The CHARINDEX AND PATINDEX came out to be equal in performance comparison. The two queries search for CREATE pattern in sys.sql_modules.definition column and both are equally good as per the execution plan.
SQL Charindex Special Characters
This is readily performed using the SUBSTRING and CHARINDEX functions. We’re fortunate in that the @ sign is distinct enough to be our “special character” to look for.
The CHARINDEX method “searches an expression for another expression and returns its starting point if found,” while the SUBSTRING function returns a portion of a string.
The @ sign can be found using the charindex function, which is then used to extract the characters before the @ sign using the substring function.
The CHARINDEX function in SQL Server is used to locate the first or starting place of an expression or characters in a string. To put it another way, it’s like looking for a specific character or letters in a string.
Syntax:
CHARINDEX (expression_to_find, expression_to_search, start_location)
The CHARINDEX function has three parameters by default. Optional is the third and final parameter, which is an Integer value.
Parameters:
1) expression_to_find
The first parameter is a character or a string of characters in which we wish to look for in another string.
Let’s say we have a string called Light Years Away and we want to know where the word Years is in the string. As a result, the function’s first parameter will be the word Years.
SELECT CHARINDEX('YEARS', 'Light Years Away') Location
This query returns a result of 7, indicating that the word Years begins at position seven in the above string. The function will discover the first location of the expression in a sentence like this, even if a word or character appears many times. Later occurrences will be overlooked.
2) expression_to_search:
A phrase or a sentence with letters such as the alphabet, numbers, and other special characters might be called an expression. A sentence with several characters is usually the second parameter in the function CHARINDEX. A variable or a column name can also be used as a parameter.
3) start_location:
The third parameter is an Integer value that is optional (you may or may not use it). You must put the figure if you want to locate or find an expression (expression_to_find) starting from a specific point in a string (expression_to_ search). If the third parameter is not specified in the function, the search begins at position 0.
Example 1: Here in our example, we have a list of categories separated by the symbol pipe |.
PHP | JAVA | PYTHON
The query to find the first position of the symbol “|” will be as follows.
SELECT CHARINDEX('|', 'PHP | JAVA | PYTHON') Location
Output:
5
Example 2: Use of start_location
We need to locate the position of the second pipe in the string because there are three categories separated by two pipes. To accomplish this, we must include a start_location after the first pipe, as the default search will begin at 0 and terminate with location 5.
SELECT CHARINDEX('|', 'PHP | JAVA | PYTHON', 6) Location
Output:
12
Example 3: Use of multiple CHARINDEX function in a single query.
Continuing with example 2, let us assume we do not have the start_location before hand an we have to find it dynamically.
SELECT CHARINDEX('|', 'PHP | JAVA | PYTHON',
CHARINDEX('|', 'PHP | JAVA | PYTHON') + 1) Location
Or
WITH TAG AS (SELECT 'PHP | JAVA | PYTHON' AS Category)
SELECT CHARINDEX('|', Category, CHARINDEX('|', 'PHP | JAVA | PYTHON') + 1) Location
FROM TAG;
Output:
12
Example 4: Ok, we found locations of the pipe. Now, using the locations we need to extract a value from the above-mentioned string.
WITH TAG AS (SELECT 'PHP | JAVA | PYTHON' AS Category)
SELECT SUBSTRING(Category, 1, CHARINDEX('|', Category) - 1) Category
FROM TAG;
Example 5: Here’s a quick technique to get (extract) all of the characters in a string before a special character. We have a list of email addresses in our instance, and we want to extract the name before the @domain.
Someone from the network team needs to gather all of the network usernames, which just so happen to be the same as their email address. By pulling only the name from the email address, we can assist them.
SELECT EmailAddress
FROM [dbo].[DimEmployee]
Example 6: The @ sign can be found using the charindex function, which is then used to extract the characters before the @ sign using the substring function. Although that is a mouthful, the query below demonstrates how simple it is.
SELECT EmailAddress
,SUBSTRING(emailaddress, 0, charindex('@', emailaddress, 0))
FROM [dbo].[DimEmployee]
You don’t always have to construct your own or write procedural code to obtain the results you need in SQL Server because there are many strong built-in functions.
SQL Charindex Substring
The CHARINDEX() function returns the position of the substring within the specified string. This works the opposite of the SUBSTRING function. Unlike the SUBSTRING() function, this function starts the search from a specified position and returns the position of the substring. If a substring is not found, it returns zero. The CHARINDEX() function is used to perform case-sensitive and case-insensitive searches based on the collation specified in the query.
Syntax:
Following is the syntax of the CHARDINDEX() function.
CHARINDEX(substring, input_string [, start_location])
CHARINDEX() accepts three arguments:
- Substring: This argument defines the substring which you need to go looking inside the enter string.
- Input_string: This argument defines the enter string.
- Start_location: This argument defines the vicinity from that you need to begin the quest inside the enter string. The information kind of this parameter is an integer, and that is an non-compulsory parameter. If this parameter isn’t always specified, then the quest begins offevolved from the start of the enter string.
Example 1: Use CHARINDEX using literal string input
Check the position of the “example” substring from the input string “This is CHARINDEX example”. To do that, execute the following query:
SELECT Charindex('example', 'This is CHARINDEX example')AS Output;
Output:
19
Now, execute the following query:
SELECT Charindex('examples', 'This is CHARINDEX example')AS Output
Output:
0
Here output will not display because examples not available in the index.
Example 2: In the below example, we retrieve the position of substring simmanchith.com using the CHARINDEX. It returns the starting position of the substring as 16. In the earlier example of the SUBSTRING function, we specified the starting position 16 to returns the string.
For example, in the below query, we use the COLLATE function along with the collation latin_general_cs_as.
SELECT Charindex('sqlshack.com',
'This is SQLSHACK.COM' COLLATE latin1_general_cs_as)
AS Output;
It returns zero in the output because it considers sqlshack.com separate from SQLSHACK.COM.
Example 3: CHARINDEX function can also perform case-sensitive searches. We need to use the COLLATE() function for a case-sensitive search.
COLLATE() function: Let’s change the substring in the capital letter to match with the string.
SELECT Charindex ('SQLSHACK.COM',
'This is SQLSHACK.COM' COLLATE latin1_general_cs_as)
AS Output;
It returns the position of SUBSTRING using the case-sensitive search using the CHARINDEX function.
Example 4: Here Add an optional starting position in the CHARINDEX() function. For example, in the below query, the 2nd query specifies a starting position at 8. Therefore, it starts looking for the substring from the 8th character position.
SELECT CHARINDEX('SQLSHACK', 'SQLSHACK - SQLSHACK.COM')
SELECT CHARINDEX('SQLSHACK', 'SQLSHACK - SQLSHACK.COM',8)
SQL Charindex from right
CHARINDEX makes it simple to find the first instance of a string in another string. It can also provide a simple technique of determining the final appearance of a string by reversing the order of the string.
For instance if a string contains the full path to a file, such as:
C:Program FilesMicrosoft SQL ServerMSSQLDATAAdventureWorks_Data.mdf
This technique can be used to extract just the filename (i.e. the part after the last ”) :
AdventureWorks_Data.mdf
Example: By searching for the location of the last occurrence of the delimiter ” we can easily strip off just the filename using the RIGHT function. The following SQL does just that:
DECLARE @FullPath VARCHAR(200)
SET @FullPath =
'C:Program FilesMicrosoft SQL ServerMSSQLDATAAdventureWorks_Data.mdf'
SELECT RIGHT(@FullPath , CHARINDEX ('' ,REVERSE(@FullPath))-1)
If we run this query it returns just the filename.
Example 2: In this case, CHARINDEX is looking for the last ” character in the inverted string. All characters to the right of this position are subsequently extracted using the RIGHT function.
The following query uses this technique to extract the file name from the full path name in the sys.master_files system view:
SELECT physical_name
,RIGHT(physical_name,CHARINDEX('',REVERSE(physical_name))-1) file_name
FROM sys.master_files
SQL Charindex Email Address
As a DBA, you may be asked to extract the domain of an email address from a database table. You may count the number of extracted domains from Email in SQL Server if you want to count the most often used domain names from email addresses in any dataset.
Example 1: SQL Queries could be used to extract the domain from the Email address.
Let us created table named “email_demo”:
create table (ID int, Email varchar (200));
Inserting values in the table email_demo:
insert into email_demo values(
(1, 'Sam@gfg.com'), (2, 'Khushi@gfg.com'),
(3, 'Komal@gfg.org'), (4, 'Priya@xyz.com'),
(5, 'Sam@xyz.com'), (6, 'Krish@xyz.com'),
(7, 'Neha@gfg.com'), (8, 'Pam@gfg.com'),
(9, 'Mohan@abc.com'), (10, 'Ankit@mno.com'),
(11, 'Kumari@gfg.com'), (12, 'Hina@abc.com'),
(13, 'Jaya@mno.com'), (14, 'Piyush@abc.com'),
(15, 'Khushi@xyz.com'), (16, 'Mona@gfg.org'),
(17, 'Roza@abc.com'));
Displaying the table contents:
select * from email_demo;
Output:
ID | |
---|---|
1 | Sam@gfg.com |
2 | Khushi@gfg.com |
3 | Komal@gfg.org |
4 | Priya@xyz.com |
5 | Sam@xyz.com |
6 | Krish@xyz.com |
7 | Neha@gfg.com |
8 | Pam@gfg.com |
9 | Mohan@abc.com |
10 | Ankit@mno.com |
11 | Kumari@gfg.com |
12 | Hina@abc.com |
13 | Jaya@mno.com |
14 | Piyush@abc.com |
15 | Khushi@xyz.com |
16 | Mona@gfg.org |
17 | Roza@abc.com |
Method 1: Extract Domain From Email in SQL Server :
In below example we will use SUBSTRING function to select the string after the @ symbol in each of the value.
SQL Extract Domain From Email:
SELECT ID,
SUBSTRING ([Email], CHARINDEX( '@', [Email]) + 1,
LEN([Email])) AS [Domain]
FROM [email_demo];
Output:
ID | Domain |
---|---|
1 | gfg.com |
2 | gfg.com |
3 | gfg.org |
4 | xyz.com |
5 | xyz.com |
6 | xyz.com |
7 | gfg.com |
8 | gfg.com |
9 | abc.com |
10 | mno.com |
11 | gfg.com |
12 | abc.com |
13 | mno.com |
14 | abc.com |
15 | xyz.com |
16 | gfg.org |
17 | abc.com |
Approached used:
- In the SUBSTRING function, we allocated the Source to our Column Name ‘Email.’
- Then, after finding the @ symbol with the CHARINDEX function, we added 1 to make the starting point after the @ symbol.
- Then, to define the end value, we used the LEN function.
Example 2: Count the number of extracted Domain From Email in SQL Server:
Approach 1: SQL Query to Count Number of Records for Extract Domain name From Email:
SELECT RIGHT ([Email],
LEN([Email]) - CHARINDEX( '@', [Email])) AS [Domain],
COUNT(*) AS [Total Number of Domain]
FROM [email_demo]
WHERE LEN([Email ]) > 0
GROUP BY RIGHT([Email],
LEN([Email]) - CHARINDEX( '@', [Email]));
Output:
Domain | Total Number of Domain |
---|---|
abc.com | 4 |
gfg.com | 5 |
gfg.org | 2 |
mno.com | 2 |
xyz.com | 4 |
Approach 2: SQL Query to Count Number of Records for Extract Domain name From Email:
SELECT SUBSTRING ([Email],
CHARINDEX( '@', [Email] ) + 1, LEN([Email])) AS [Domain],
COUNT(*) AS [Total Number of Domain]
FROM [email_demo]
WHERE LEN([Email]) > 1
GROUP BY SUBSTRING ([Email], CHARINDEX( '@', [Email] ) + 1,
LEN([Email]));
Output:
Domain | Total Number of Domain |
---|---|
abc.com | 4 |
gfg.com | 5 |
gfg.org | 2 |
mno.com | 2 |
xyz.com | 4 |
Example 3: SQL Extract Domain From Email Example:
The SUBSTRING function allows you to extract and display the part of a string. In this SQL Server example, we will show you how to Select string after the @ symbol in each record.
SELECT SUBSTRING (
[Email Adress],
CHARINDEX( '@', [Email Adress] ) + 1,
LEN([Email Adress])
) AS [Domain Name]
FROM [EmailAdress]
In general, the SUBSTRING Function allows three parameters, and they are Source, start point, endpoint. Here we assigned the Source as our Column Name:
Next, we used the CHARINDEX Function to find the @ symbol, and then we added 1. so that the starting point will be after the @ symbol.
Lastly, we used the LEN Function in SQL Server to specify the end value.
SUBSTRING (
[Email Adress], -- Source
CHARINDEX( '@', [Email Adress] ) + 1, -- Start Point
LEN([Email Adress] -- End Point
)
Example 4: Extract Domain From Email Example
We show how to use the Right Function to extract the domain name from the email address.
SQL Query to Extract Domain name From Email and Count Number of Records
SELECT RIGHT (
[Email Adress],
LEN([Email Adress]) - CHARINDEX( '@', [Email Adress] )
) AS [Domain Name],
COUNT(*) AS [Total Records with this Domain]
FROM [EmailAdress]
WHERE LEN([Email Adress]) > 0
GROUP BY RIGHT (
[Email Adress],
LEN([Email Adress]) - CHARINDEX( '@', [Email Adress] )
)
Example 5: Telephone numbers have a fixed structure, but email addresses are a bit more tricky to parse since you don’t know their exact length upfront. An email address has the following format:
< recipient>@domain
Where domain = < domain name>.< top-level domain>
In this example, we’re assuming there’s only one @ symbol present in the email address. Technically, you can have multiple @ symbols, where the last one is the delimiter between the recipient and the domain. This is for example a valid email address: “user@company”@company.com. This is out of scope for this tip.
Using the CHARINDEX function, we can find the position of the @. The recipient can then be found by taking the start of the email address right until the @ symbol. The domain is everything that comes after the @ symbol. If you also want to extract the top-level domain, you cannot search for the first dot starting from the right, since some top-level domains have a dot, for example co.uk. Let’s see how we can parse the email address john.doe@mssqltips.co.uk.
WITH cte_sample AS
(
SELECT email = 'john.doe@mssqltips.co.uk'
)
SELECT
email
,recipient = SUBSTRING(email,1,CHARINDEX('@',email,1) - 1)
,fulldomain = SUBSTRING(email,CHARINDEX('@',email,1) + 1,LEN(email))
,domainname = SUBSTRING( email
,CHARINDEX('@',email,1) + 1 -- start is one char after the @
, -- starting position of charindex is the position of @
CHARINDEX('.',email,CHARINDEX('@',email,1))
- CHARINDEX('@',email,1)
-- length is the position of the first dot after the @ - position of the @
)
,toplevel = SUBSTRING( email
,CHARINDEX('.',email,CHARINDEX('@',email,1)) + 1 -- position of first dot after @
,LEN(email)
)
FROM [cte_sample];
We provided LEN(email) for both the complete domain and the top-level domain, which is too long. If the SUBSTRING length is more than the number of characters, everything is returned until the last character. This saves us from having to write a more difficult expression to calculate the correct length when all we need is the substring from a specific location to the end of the text.
Charindex Case Insenstive OR Ignore Case Sensitive
The CHARINDEX() function returns the location of a substring found in a string.
This function returns 0 if the substring cannot be found.
Note that this function searches without regard for case.
Syntax:
CHARINDEX(substring, string, start)
Parameter Values
Parameter | Description |
---|---|
substring | Required. The substring to search for |
string | Required. The string to search for |
start | Optional. The position where the search will start (if you do not want to start at the beginning of string). The first position in string is 1. |
Example 1: Search for “OM” in string “Customer”, and return position:
SELECT CHARINDEX('OM', 'Customer') AS MatchPosition;
Example 2: You can explicitly perform a case-sensitive search by adding the COLLATE clause to your SELECT statement:
Here’s an example that includes a case-sensitive search and a case-insensitive search:
SELECT
CHARINDEX('Beer', 'Bob likes beer.' COLLATE Latin1_General_CS_AS) AS 'Case-Sensitive',
CHARINDEX('Beer', 'Bob likes beer.' COLLATE Latin1_General_CI_AS) AS 'Case-Insensitive';
Result:
Case-Sensitive | Case-Insensitive |
---|---|
0 | 11 |
The first one is case-sensitive because _CS (in the collation) stands for Case-Sensitive. The second one is case-insensitive because _CI stands for Case-Insensitive.
Example 3: This statement shows a case-insensitive search for the string ‘SERVER’ in ‘SQL Server CHARINDEX’:
SELECT CHARINDEX(
'SERVER',
'SQL Server CHARINDEX'
) position;
Output:
SQL Charindex – nth Occurence
Example 1: The CHARINDEX() method in SQL is useful for extracting characters from a string. However, it only returns the first time a character appears. It’s common to need to find the Nth instance of a character or a space, which can be a difficult process in traditional SQL. This is possible thanks to a method I created called CHARINDEX2(). The target string or character, the string to be searched, and the occurrence of the target string/character are all passed in as parameters.
SELECT dbo.CHARINDEX2('a', 'abbabba', 3)
Returns the location of the third occurrence of ‘a’
7
CREATE FUNCTION CHARINDEX2
(
@TargetStr varchar(8000),
@SearchedStr varchar(8000),
@Occurrence int
)
RETURNS int
as
begin
declare @pos int, @counter int, @ret int
set @pos = CHARINDEX(@TargetStr, @SearchedStr)
set @counter = 1
if @Occurrence = 1 set @ret = @pos
else
begin
while (@counter < @Occurrence)
begin
select @ret = CHARINDEX(@TargetStr, @SearchedStr, @pos + 1)
set @counter = @counter + 1
set @pos = @ret
end
end
RETURN(@ret)
end
Example 2: After specifying the number of occurrences (nth) of the same character, this function returns the position or index of characters in a string:
CREATE FUNCTION CharIndexAfterN (
@Char VARCHAR(10)
,@String VARCHAR(MAX)
,@Occurance INT
)
RETURNS INT
AS
BEGIN
DECLARE @Index AS INT = 1
WHILE @Occurance <> 0
BEGIN
SET @Index = CHARINDEX(@Char, @String, @Index + 1)
SET @Occurance -= 1
END
RETURN @Index
END
For example:
SELECT dbo.CharIndexAfterN('ab', '***ab****ab*******ab*', 2)
Output:
10
Example 3: I’ve got a string and I’d like to get the text between two letters. However, the letters exist multiple times in the string, and I want to return the text between the Nth and Nth appearances of the character.
In this instance, I have a table of MDX members and I’d like to get the member’s code. The text between the final (fourth) square brackets is the code.
Set up some testing data:
CREATE TABLE #T
([TestColumn] nvarchar(200) NOT NULL);
INSERT INTO #T
([TestColumn])
VALUES
('[Countries Regions Cities].[Countries].[Country].&[CN]'),
('[Countries Regions Cities].[Countries].[Region].&[4]');
I have two solutions. One uses a recursive CTE and the other uses CROSS APPLY.
APPROACH 1:
Using Recursive CTE: The occurrence variable sets the Nth term I want to return.
DECLARE @start_character nvarchar(1);
DECLARE @end_character nvarchar(1);
DECLARE @occurence int;
SET @start_character = '[';
SET @end_character = ']';
SET @occurence = 4;
WITH T AS
(
SELECT 1 AS [Occ],
charindex(@start_character, [TestColumn]) AS pos1,
charindex(@end_character, [TestColumn]) AS pos2,
[TestColumn]
FROM #T
UNION ALL
SELECT [Occ] + 1 AS [Occ],
charindex(@start_character, [TestColumn], pos1 + 1) AS pos1,
charindex(@end_character, [TestColumn], pos2 + 1) AS pos2,
[TestColumn]
FROM T
WHERE pos1 > 0
)
SELECT [Occ],
[TestColumn],
substring([TestColumn], pos1 + 1, pos2 - pos1 - 1) AS [Text]
FROM T
WHERE [Occ] = @occurence;
APPROACH 2:
Using CROSS APPLY
This actually returns the hierarchy name, but to return the code you just have to add another APPLY that looks for the third position:
DECLARE @character1 nvarchar(1);
DECLARE @character2 nvarchar(1);
SET @character1 = '[';
SET @character2 = ']';
SELECT [TestColumn],
substring([TestColumn], P3.Pos + 1, C3.Pos - P3.Pos - 1) AS [Text]
FROM #T
CROSS APPLY (SELECT (charindex(@character1, [TestColumn]))) AS P1(Pos)
CROSS APPLY (SELECT (charindex(@character2, [TestColumn]))) AS C1(Pos)
CROSS APPLY (SELECT (charindex(@character1, [TestColumn], P1.Pos+1))) AS P2(Pos)
CROSS APPLY (SELECT (charindex(@character2, [TestColumn], C1.Pos+1))) AS C2(Pos)
CROSS APPLY (SELECT (charindex(@character1, [TestColumn], P2.Pos+1))) AS P3(Pos)
CROSS APPLY (SELECT (charindex(@character2, [TestColumn], C2.Pos+1))) AS C3(Pos);
The efficiency of the two solutions does not appear to differ significantly. The Recursive CTE can readily expand to enormous strings with a large number of recurrences of a character (i.e. N is vast), whereas the APPLY technique would become extremely verbose. The APPLY method, on the other hand, is well suited to returning more data from the same row (i.e. I could return both the 3rd and 4th occurrences on the same row).
We use the SQL CHARINDEX function to find the position of a substring or expression in a given string. We might have a character in different positions of a string. SQL CHARINDEX returns the first position and ignores the rest of matching character positions in a string.
SQL CHARINDEX Function Syntax
CHARINDEX ( expression_to_find , expression_to_search [ , start_location ] ) |
It takes following parameters in SQL CHARINDEX function.
- expression_to_find: In this parameter, we specify a character or string that we want to search in another string
- expression_to_search: We can specify a string or sentence in which we want to search expression_to_find
- start_location: It is an optional parameter. We can specify an integer value in this parameter to specify start location. If we want to search expression_to_find in expression_to_search with a specified start location, we can specify it. By default, if we do not mention any value in this parameter, it starts a search from the index position 0
Example 1: Search a character position in a string
In this example, we want to find position of @ character in a specified email address rajendra.gupta16@gmail.com
In the following screenshot, we can see position of each character in the email address string. The character ‘@’ is in position 17. We get this position in output of SQL CHARINDEX.
SELECT CHARINDEX (‘@’,‘rajendra.gupta16@gmail.com’) as ‘CharacterPosition’ |
Example 2: Use of optional parameter Start_position in SQL CHARINDEX
Similarly, let’s search dot (.) position in this string. We can see that the dot is on position 9th and 23rd in the specified string (email address).
SELECT CHARINDEX(‘.’,‘rajendra.gupta16@gmail.com’,11) as ‘CharacterPosition’ |
Once we execute this script, it returns the first position of the dot in the output. It starts the search from starting position of a string and stops once it finds a suitable match.
Suppose we want to get the position of the second dot (.) in this email address. We can specify a value for an optional parameter to start searching from a specific position. It starts from a specific character position and checks for the character position. We still get the actual position of the character that is 23rd in this example.
Example 3: Search a substring position in a specified string in SQL CHARINDEX
In previous examples, we searched a specific character in a specified string. We can also search a substring as well in a string.
In the following query, we declared a variable @ExpressionToSearch for the string and set a value on it. We want to search for substring Rajendra in this string.
DECLARE @ExpressionToSearch varchar(100) SET @ExpressionToSearch = ‘Explore SQL Server on SQLShack with Rajendra Gupta articles’ SELECT CHARINDEX (‘Rajendra’, @ExpressionToSearch) AS ‘CharacterPosition’ |
It searches for the substring in a specified string. If it gets an exact match of the substring, it returns the starting position of the substring.
If an exact match is not found it returns 0 in the output.
Example 4: Search a substring position in a specified string with multiple matching in SQL CHARINDEX
Suppose we want to search a substring in a specified string. In the string we have multiple matching substrings. For example, in the following query, we want to search for SQLShack and find its position.
DECLARE @ExpressionToSearch varchar(100) SET @ExpressionToSearch = ‘Explore SQL Server on SQLShack with Rajendra Gupta articles – SQLShack’ SELECT CHARINDEX (‘SQLShack’, @ExpressionToSearch) AS ‘CharacterPosition’ |
We can use start_location in SQL CHARINDEX with a substring as well. For example, let’s start with position 24 and see the result. It starts from character position 24 and searches for a particular substring. We can see the substring starting position is now at 63.
DECLARE @ExpressionToSearch varchar(100) SET @ExpressionToSearch = ‘Explore SQL Server on SQLShack with Rajendra Gupta articles – SQLShack’ SELECT CHARINDEX (‘SQLShack’, @ExpressionToSearch) AS ‘CharacterPosition’ |
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.
DECLARE @Name AS VARCHAR(100)= ‘Explore SQL Server with articles on SQLShack’; SELECT CASE WHEN CHARINDEX(‘SQLShack’, @Name) > 0 THEN ‘Exists’ ELSE ‘Not Exists’ END AS FindSubString; SELECT CASE WHEN CHARINDEX(‘Rajendra’, @Name) > 0 THEN ‘Exists’ ELSE ‘Not Exists’ END AS FindSubString; |
In the following screenshot, we can see that SQL CHARINDEX function checks for a particular substring. If it returns a value greater than 0 it means substring exists in specified string else it does not exist.
- Substring SQLShack exists in a specified string that’s why the output is Exists
- Substring Rajendra does not exist in a specified string that’s why the output is Not Exists
Example 6: Case sensitive search with SQL CHARINDEX
In the previous examples, we did not use case sensitive search. For example, in the following query, we want to search for substring sqlshack in our string. This substring exists does but it exists in upper case.
DECLARE @ExpressionToSearch varchar(100) SET @ExpressionToSearch = ‘Explore SQL Server on SQLSHACK with Rajendra Gupta articles – SQLShack’ SELECT CHARINDEX (‘sqlshack’, @ExpressionToSearch) AS ‘CharacterPosition’ |
It does not perform case sensitive search, and we still get the correct output.
We can use collation to perform case sensitive search. In the following examples, we use COLLATE Latin1_General_CS_AS to perform case sensitive search. We need to note that all character case in a substring should match within a string. Execute the following query to understand this.
DECLARE @ExpressionToSearch varchar(100) SET @ExpressionToSearch = ‘Explore SQL Server on SQLSHACK with Rajendra Gupta articles ‘ SELECT CHARINDEX (‘sqlshack’, @ExpressionToSearch COLLATE Latin1_General_CS_AS) AS ‘CharacterPosition’ SELECT CHARINDEX (‘SQLShack’, @ExpressionToSearch COLLATE Latin1_General_CS_AS) AS ‘CharacterPosition’ SELECT CHARINDEX (‘SQLSHACK’, @ExpressionToSearch COLLATE Latin1_General_CS_AS) AS ‘CharacterPosition’ |
Example 7: SQL CHARINDEX and table column
We can use SQL CHARINDEX for existing data in a table. We can use it to get output in a separate column. In the following example, we want to check the position of character R in empname column values of the Employee table.
SELECT TOP 10 [EmpName], CHARINDEX(‘R’, empname) AS “Position of R“, [City], [Designation] FROM [SQLShackDemo].[dbo].[Employee]; |
In the following screenshot, we have a new column to get position of character R in EmpName column values. If Empname does not contain specified character R, it returns 0.
Let’s update one record in Employee table and replace empname with NULL.
Update [SQLShackDemo].[dbo].[Employee] set EmpName=NULL where empname=‘Charlotte Robinson’ |
Rerun the SQL CHARINDEX query, and we see value NULL in Position of R column. If we have a NULL value in a column, it also returns a NULL value.
Example 8: SQL CHARINDEX and Numeric value
We can search for numeric value as well as using SQL CHARINDEX. Suppose we want to find a position of character 1 in empid column of the employee table.
SELECT TOP 10 [EmpName],EmpID, CHARINDEX(‘1’, empid) AS “Position of 1“, [City], [Designation] FROM [SQLShackDemo].[dbo].[Employee]; |
We need to specify numeric value as well in single quotes. If we do not put single quotes, it gives following error message.
Msg 8116, Level 16, State 1, Line 2 Argument data type int is invalid for argument 1 of charindex function. |
Quick Recap of SQL CHARINDEX
- SQL CHARINDEX Returns a position of a substring within a string
- If the target string varchar(max), nvarchar(max), it returns Big Int value else it returns Int data type
- By default, it performs a case insensitive search
- If there is no match found, it returns 0 in return
Conclusion
In this article, we explored SQL CHARINDEX function and its usage with various examples. Please feel free to provide feedback or ask questions in the comment section below.
- Author
- Recent Posts
Hi! I am Rajendra Gupta, Database Specialist and Architect, helping organizations implement Microsoft SQL Server, Azure, Couchbase, AWS solutions fast and efficiently, fix related issues, and Performance Tuning with over 14 years of experience.
I am the author of the book “DP-300 Administering Relational Database on Microsoft Azure”. I published more than 650 technical articles on MSSQLTips, SQLShack, Quest, CodingSight, and SeveralNines.
I am the creator of one of the biggest free online collections of articles on a single topic, with his 50-part series on SQL Server Always On Availability Groups.
Based on my contribution to the SQL Server community, I have been recognized as the prestigious Best Author of the Year continuously in 2019, 2020, and 2021 (2nd Rank) at SQLShack and the MSSQLTIPS champions award in 2020.
Personal Blog: https://www.dbblogger.com
I am always interested in new challenges so if you need consulting help, reach me at rajendra.gupta16@gmail.com
View all posts by Rajendra Gupta
Summary: in this tutorial, you will learn how to use the SQL Server CHARINDEX()
function to search for a substring in a string.
SQL Server CHARINDEX()
function searches for a substring inside a string starting from a specified location. It returns the position of the substring found in the searched string, or zero if the substring is not found. The starting position returned is 1-based, not 0-based.
The following shows the syntax of the CHARINDEX()
function:
CHARINDEX(substring, string [, start_location])
Code language: SQL (Structured Query Language) (sql)
In this syntax:
substring
is the substring to search for. Its length is limited to 8,000 characters.string
can be a literal string, expression or column. It is a string to search.start_location
is the location at which the search starts. Thestart_location
is an integer, big integer or an expression that evaluates to a value of those data types.
The start_location
parameter is optional. If it is skipped, zero, or negative value, the search starts at the beginning of the string
.
Note that the CHARINDEX()
function can perform both case-sensitive and case-insensitive searches based on the specified collation.
SQL Server CHARINDEX()
function examples
Let’s take some examples of using the CHARINDEX()
function.
A) Using CHARINDEX()
to perform a single search
The following example uses the CHARINDEX()
function to perform a simple search of the string 'SQL'
in the 'SQL Server CHARINDEX'
SELECT
CHARINDEX('SQL', 'SQL Server CHARINDEX') position;
Code language: SQL (Structured Query Language) (sql)
Here is the output:
position
-----------
1
(1 row affected)
B) Using CHARINDEX()
function to perform a case-insensitive search
This statement shows a case-insensitive search for the string 'SERVER'
in 'SQL Server CHARINDEX'
:
SELECT
CHARINDEX(
'SERVER',
'SQL Server CHARINDEX'
) position;
Code language: SQL (Structured Query Language) (sql)
Here is the output:
position
-----------
5
(1 row affected)
C) Using CHARINDEX()
function to perform a case-sensitive search
The following example shows a case-sensitive search for the string 'SERVER'
in searched string SQL Server CHARINDEX
.
SELECT
CHARINDEX(
'SERVER',
'SQL Server CHARINDEX'
COLLATE Latin1_General_CS_AS
) position;
Code language: SQL (Structured Query Language) (sql)
Here is the output:
position
-----------
0
(1 row affected)
D) Using CHARINDEX()
function to search for a nonexistent substring
The following example illustrates a search for the substring 'needle'
in the string 'This is a haystack'
DECLARE @haystack VARCHAR(100);
SELECT @haystack = 'This is a haystack';
SELECT CHARINDEX('needle', @haystack);
Code language: SQL (Structured Query Language) (sql)
The output is:
position
-----------
0
(1 row affected)
Code language: SQL (Structured Query Language) (sql)
E) Using CHARINDEX()
function to searching from a specific position
This example uses the start_location
parameter to start the search for 'is'
at the fifth and tenth character of the string 'This is a my sister'
:
SELECT
CHARINDEX('is','This is a my sister',5) start_at_fifth,
CHARINDEX('is','This is a my sister',10) start_at_tenth;
Code language: SQL (Structured Query Language) (sql)
Here is the output:
start_at_fifth start_at_tenth
-------------- --------------
6 15
(1 row affected)
In this tutorial, you have learned how to use the SQL Server CHARINDEX()
function to search for a substring in a string starting from a specified location and return the position of the substring.