SQL Server: SQL like wildcard special usage: Escape

Source: Internet
Author: User

 SQL Server: SQL like wildcard special usage: Escape

 

%: Match zero or multiple arbitrary characters; _: match any single character; []: match a range; [^]: exclude a range

 

symbol meaning
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
like '[^ 1-9]' 0
like '[^ 1-9b-z]' 0, a

For special characters in a string: '%', '[', '[]', '_', you can use '[]' to include them, in this way, pattern is treated as normal characters.

1. Use like '[[]' to match the special character '['

 
Select 1 Where '[ABCDE 'like' [[] %'

2. Use like ']' to match the special character ']'

 
Select 1 Where '] ABCDE 'like'] %'

3. Use like '[[]' to match the special character '[]'.

Select 1 Where '[] ABCDE' like '[[] %'

4. Use like '[_]' to match the special character '_'

 
Select 1 Where '_ ABCDE' like '[_] %'

5. Use like '[%]' to match the special character '%'

 
Select 1 where 'abc % de' like 'abc [%] de'

For other special characters: '^', '-', ']', because they are used in '[]', they must be escaped in another way, the like clause is introduced, and it is worth noting that escape can escape all special characters.

 
Select 1 Where '^ ABCDE 'like '! ^ ABCDE 'escape '! 'Select 1 where'-ABCDE 'like '! -ABCDE 'escape '! 'Select 1 where'] ABCDE 'like'!] ABCDE 'escape '! 'Select 1 where' % ABCDE 'like' \ % ABCDE 'escape '\ 'select 1 where' % ABCDE 'like '! % ABCDE 'escape '! 'Select 1 where' % ABCDE 'like' # % ABCDE 'escape '# 'select 1 where' % ABCDE 'like' @ % ABCDE 'escape' @ 'select 1 where '[ ABCDE 'like '! [ABCDE 'escape '! 'Select 1 where'] ABCDE 'like'!] ABCDE 'escape '! '

As you can see, we can use the characters followed by escape as escape characters. The character after escape is equivalent to the escape character '\' in the C-language string '\'.

Finally, let's look at a more complex match.

 
Select 1 Where '[^ A-Z] ABCDE 'like' \ [\ ^ A \-Z \] % 'escape '\'

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.