Wildcards in SQL statements

Source: Internet
Author: User
Wildcard:
Wildcard
Description
Example
%
Any string containing zero or more characters.
WHERE title LIKE '% computer %' searches for all titles containing the word computer in any location of the title.
_ (Underline)
Any single character.
WHERE au_fname LIKE '_ ean' searches for the names of all four letters ending with ean (such as Dean and Sean ).
[]
Specifies any single character in the range ([a-f]) or set ([abcdef.
WHERE au_lname LIKE '[C-P] arsen' will look for the author's surname that ends with arsen and starts with any single character between C and P, such as Carsen, Larsen, Karsen, and so on.
[^]
It does not belong to any single character in the specified range ([a-f]) or set ([abcdef.
WHERE au_lname LIKE 'de [^ l] %' searches for the last names of all authors whose names start with de and whose subsequent letters are not l.
Use wildcards as text
The wildcard pattern matching string can be used as a text string by placing the wildcard in parentheses. The following table shows examples of using the LIKE keyword and the [] wildcard.
Symbol
Description
LIKE '5 [%]'
5%
LIKE '[_] N'
_ N
LIKE '[a-cdf]'
A, B, c, d, or f
LIKE '[-acdf]'
-, A, c, d, or f
LIKE '[[]'
[
LIKE ']'
]
LIKE 'ABC [_] d %'
Abc_d and abc_de
LIKE 'ABC [def]'
Abcd, abce, and abcf
Use the ESCAPE clause pattern matching
You can search for strings that contain one or more special wildcards. For example, the discounts table in the MERS database may store discount values containing percent signs (%. To search for a percent sign as a character rather than a wildcard character, you must provide the ESCAPE keyword and ESCAPE character. For example, a sample database contains a column named comment, which contains 30% text. To search for any row containing string 30% at any position in the comment column, specify WHERE comment LIKE '% 30! % 'Escape '! 'Where clause. If ESCAPE and ESCAPE characters are not specified, SQL Server returns all rows containing string 30.
The following example shows how to search for the string "50% off when 100 or more copies are purchased" in the notes column of the pubs database titles table ":
SELECT notes FROM titles WHERE notes LIKE '2014% off when 50% or more copies are purchased 'escape '%'

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.