T-SQL character escape

Source: Internet
Author: User

I found the problem when I used SQL Server to query the table name starting with PRM _, check whether the underline is not escaped, I would like to summarize the T-SQL character escape

Create a temporary table and insert sample data

 1   Create   Table # Characters (CH Varchar ( 200  ))  2   Insert # Characters Values ( '  [Start time ]:  '  )  3  Insert # Characters Values ( '  Prm_batch  '  )  4   Insert # Characters Values ( '  Select promisetime  ''  Ptime  '''  )  5  Insert # Characters Values ( '  4% 2 = 0  '  )  6   Insert # Characters Values ( '  4/2 = 2  ' )

Escape Query

 1   --  Escape with/and escape [] 2   Select   *   From # Characters Where Ch Like   '  /[Start time/] %  '   Escape   '  /  '  3   --  For _ and %, escape []  4  Select   *   From # Characters Where Ch Like   '  PRM [_] %  '  5   Select   *   From # Characters Where Ch Like   '  4 [%] % '  6   --  For single quotes, add single quotes to escape  7   Select   *   From # Characters Where Ch Like   '  Select promisetime  ''  %  '  8   -- No escape is required /.  9   Select   *   From # Characters Where Ch Like   '  4/%  ' 

 

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.