The Escape Character of SQL is: '(single quotes)

Source: Internet
Author: User

SQLThe Escape Character of is: '(single quotes)

Example: Select * From TBL where uyear ='''06'

Note that the single quotation marks of the red background indicate escape characters. If we omit them, the entire statement will fail and the escape characters will not be output. In the preceding example, the actual condition value of uyear is '06, instead of ''06

Why can't it be omitted? If we omit it, the previous sentence becomes: Select * From TBL where uyear =''06'

BecauseSQLThe start and end symbols of the string are enclosed in single quotes?SQLThe interpreter considers the gray background in the statement as a string, and the subsequent statement is obviously an incorrect statement. Of course, an error is reported. To solve the single quotation mark problem of the string, an escape character list is displayed.

SQLThe server has two escape characters :'By default, 'is the boundary character of the string. If' is contained in the string, two 'must be used, and 1st' is the escape character.

Another Escape Character is"
WhenWhen set quoted_identifier off, "is a string boundary, and" must be expressed as two "in the string.
VB: "" <=>"SQLServer 2000: ''' <=>'

SQL Server 2000: "" <=>"

Eg:

Declare @ searchtype nvarchar (50) Declare @ searchstring nvarchar (255) Declare @ searchkey nvarchar (50) Declare @ searchsql nvarchar (2000)

Set @ searchtype = '2' set @ searchkey = 'D'

Set @ searchstring = case @ searchtype when '1' then '1 = 1'When '2' then 'p. projectname like ''' + '%' + @ searchkey + '%' + ''''When '3' then' p. projectcity like ''' + '%' + @ searchkey + '%' + ''' when' 4 'then' C. catename like ''' + '%' + @ searchkey + '%' + '''when' 4 'then' p. projectmanager like ''' + '%' + @ searchkey + '%' + ''' end

Set @ searchsql = N'

Select p. *, datename (year, projectposttime) + '+ '''-''' +' + datename (month, projectposttime) + '+ '''-''' +' + datename (day, projectposttime) '+' As posttime, M. empname, C. catename from proproject as P, mrbaseinf as M, procate c Where p. empid = m. empid and P. cateid = C. cateid and '? ? + @ Searchstring

Print (@ searchsql)

Exec (@ searchsql)

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.