SQL server charindex function and patindex function)

Source: Internet
Author: User
Tags sql server books
The charindex and patindex functions are often used to search for characters or strings in a string. If the searched characters contain the characters to be searched, the two functions return a non-zero integer, which is the start digit of the character to be searched in the searched characters. The patindex function supports the use of wildcards for search. However, charindex does not support wildcards. Next, we will analyze these two functions one by one.

How to Use the charindex Function
The charindex function returns the starting position of a character or string in another string. The charindex function is called as follows:
Charindex (expression1, expression2 [, start_location])
Expression1 is the character to be searched in expression2. start_location is the position where the charindex function starts to find expression1 in expression2.
The charindex function returns an integer that is the position of the string to be searched in the string to be searched. If charindex does not find the string to be searched, the function integer is "0 ". Let's take a look at the following function command execution results:
Charindex (SQL, microsoft SQL server)
This function command returns the starting position of "SQL" in "microsoft SQL server". In this example, the charindex function returns position 11 of "s" in "microsoft SQL server.
Next, let's look at the charindex command:
Charindex (7.0, Microsoft SQL Server 2000)

In this example, charindex returns zero because the string "7.0" cannot be found in "Microsoft SQL Server.

The following two examples show how to use the charindex function to solve the actual T-SQL problem.

In the first example, we assume that you want to display the last name of the contact column in the first five rows of the northwind database customer table. This is the first five rows of data.
Contactname
------------------------------
Maria Anders
Ana Trujillo
Antonio Moreno
Thomas hard
Christina berglund
As you can see, mmname contains the customer's first name and last name, which are separated by a space. I use the charindx function to determine the spaces in the two names. Through this method, we can analyze the space position of the contactname column, so that we can only display the last name part of this column. This is the record showing the last name of the first five rows in the customer table of northwind!
Select top 5 substring (contactname, charindex (, contactname) + 1, len (contactname) as [last name] from northwind. dbo. MERs
The following is the output result of this command.
Last name
------------------------------
Anders
Trujillo
Moreno
Hard
Berglund
The charindex function finds the space between the first name and last name, so the substring function can separate the contactname column, so that only the last name is selected. I add 1 to the integer returned by the charindex function, so that the last name does not start with a space.
In the second example, if you want to calculate the number of records in which a field contains specific characters. The charindex function can easily solve your problem. Calculate the number of records in which the addresses field in the northwind. dbo. customer table contains the word road or its abbreviated rd. The selection statement is similar to this:
Select count (*) from northwind. dbo. MERs

Where charindex (rd, address)> 0 or charindex (road, address)> 1

 

How to Use the patindex Function
The patindex function returns the starting position of a character or string in another string or expression. The patindex function supports searching strings with wildcards, which makes the patindex function very valuable for changing search strings. The command for the patindex function is as follows:
Patindex (% pattern %, expression)
Pattern is the string to be searched, and expression is the string to be searched. Generally, expression is a field in a table. "%" must be used before and after pattern unless the string you search is at the beginning or end of the string to be shrunk.
Similar to the charindex function, the patindex function returns the start position of the search string in the searched string. Suppose there is such a patindex function:
Patindex (% bc %, abcd)
The result returned by the patindex function is 2, which is the same as that returned by the charindex function. Here, the % Mark tells the patindex function to find the string "bc", regardless of the number of characters in the searched string before and after "bc!
If you want to know whether the searched string starts with a specific string, you can skip the % mark. Write the patinded function as follows:
Patindex (AB %, abcd)
The result of executing this command returns 1, indicating that the searched string "AB" is found in the searched string "abcd.
You can use wildcards to edit search strings that are much more complex than the preceding simple example. If you want to determine whether a string contains letters a and z with any numbers, the parindex FUNCTION command may look like this:
Patindex (% [a, z, 0-9] % [a, z, 0-9] % [a, z, 0-9] %, xyzabc123)
Note that many wildcard characters are used in the search character section in the preceding example. Check that SQL server books online can get more information about the pass. Next, we will use two examples to see how patindex and select can be used together.
Assume that the description field in the northwind. dbo. categories table contains any record containing the word "bread" or "bread", the selection statement may be as follows:
Select description from northwind. dbo. categories
Where patindex (% [B, B] read %, description)> 0
Here, I use wildcards to determine the upper and lower case "B ". After I run this script in the notthwind database, the following result is displayed:
Description
--------------------------------------------------------
Desserts, candies, and sweet breads
Breads, crackers, pasta, and cereal
This is an example of using an additional wildcard to find some records. In this example, the second letter of the description field is not an "e" record.
Select description from northwind. dbo. categories
Where patindex (% [B, B] read %, description)> 0
And patindex (_ [^ e] %, description) = 1
By adding a patindex function using the ^ wildcard in the Condition Statement, We can filter out the "dessert, candies, and sweet breads" record. The preceding query results have only one record.
Description
--------------------------------------------------------
Breads, crackers, pasta, and cereal
Summary
Now you can find the difference between charindex and patindex when searching strings. The patindex function supports wildcard characters and can be used in many different searches. Charindex cannot. Depending on your own situation, these two functions are helpful for searching, controlling, and analyzing strings in SQL server.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.