Use like to search for wildcard characters in SQL queries

Source: Internet
Author: User
Search for wildcard characters

You can search for wildcard characters. There are two ways to specify characters that are typically used as wildcards:

  • Use the escape keyword to define the escape character. In mode, when an escape character is placed before a wildcard, the wildcard is interpreted as a common character. For example, to search for strings containing string 5% at any position, use:

    WHERE ColumnA LIKE '%5/%%' ESCAPE '/'    

    In the like clause above, the leading and ending percent signs (%) are interpreted as wildcards, while the percent signs after the slash (/) are interpreted as characters %.

  • Square brackets ([]) contain only the wildcard character. To search for a break number (-) instead of specifying the search range, specify the break number as the first character in square brackets:
    WHERE ColumnA LIKE '9[-]5'    

    The following table shows the usage of wildcards enclosed in square brackets.

    Symbol Description
    Like '5 [%]' 5%
    Like '5%' 5 followed by a string of 0 or more characters
    Like '[_] N' _ N
    Like '_ N' An, in, on (and so on)
    Like '[A-CdF]' A, B, C, D, or F
    Like '[-ACDF]' -, A, C, D, or F
    Like '[[]' [
    Like ']' ]

If you use like to compare strings, all the characters in the pattern string, including the start space and/or trailing space, are meaningful. If the query comparison requires that all rows containing "ABC" (there is a space after ABC) be returned, the column value "ABC" (no space after ABC) is not returned. However, this is not the case. Ignore spaces at the end of the expression to be matched by the pattern. If the query comparison requires that all rows containing "ABC" (without spaces after ABC) be returned, all rows starting with "ABC" with zero or multiple trailing spaces are returned.

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.