Patindex () Usage

Source: Internet
Author: User
Patindex
Returns the starting position of a mode that appears for the first time in a specified expression. If this mode is not found in all valid text and character data types, zero is returned.
Syntax
Patindex ('% pattern %', expression)
Parameters
Pattern
A string. Wildcards can be used, but must contain % characters before and after the pattern (except when the first and last characters are searched ). Pattern is the expression of the short character data type category.
Expression
An expression is usually used to search for columns in the specified mode. expression is a string data type.
Return type
Int
Note
Patindex is useful for text data types. Except for is null, is not null, and like (these are the only comparative operations that are valid for text in the WHERE clause, patindex can also be used in the WHERE clause.

-----------------------------------------

Example 1:
Find out that the description field in the northwind. DBO. Categories table contains all records containing the word "Bread" or "Bread". Then the selection statement may be like this:

Select description from northwind. DBO. Categories
Where patindex ('% [B, B] Read %', description)> 0
In patindex, wildcards can be used to determine the upper and lower case "B"

Example 2:
Find out that the description field in the northwind. DBO. Categories table contains the word "Bread" or "Bread", and the second sub-letter is not the record of "E.

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.

Patindex and charindex

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 very helpful for searching, controlling, and analyzing strings in SQL Server.

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.