• 字符串文字中包含引号


    常常有人写SQL时不知道在字符串内的单引号怎么写,其实只要把一个单引号换成两个单引号表示就可以。

    示例如下:

    SELECT '''' AS c FROM DUAL
    UNION ALL
    SELECT 'GOO''D' AS c FROM DUAL;
    

    另外,Oracle10g开始引入了q-quote特性,使用q-quote的写法就比较明确了:

    SELECT q'[']' AS c FROM DUAL
    UNION ALL
    SELECT q'[GOO'D]' AS c FROM DUAL;
    

     

    Oracle10g的SQL参考手册中相关描述如下:

    Use the text literal notation to specify values whenever 'string' or appears in the syntax of expressions, conditions, SQL functions, and SQL statements in other parts of this reference. This reference uses the terms text literal, character literal, and string interchangeably. Text, character, and string literals are always surrounded by single quotation marks. If the syntax uses the term char, you can specify either a text literal or another expression that resolves to character data — for example, the last_name column of the hr.employees table. When char appears in the syntax, the single quotation marks are not used.

    where N or n specifies the literal using the national character set (NCHAR or NVARCHAR2 data). By default, text entered using this notation is translated into the national character set by way of the database character set when used by the server. To avoid potential loss of data during the text literal conversion to the database character set, set the environment variable ORA_NCHAR_LITERAL_REPLACE to TRUE. Doing so transparently replaces the n’ internally and preserves the text literal for SQL processing.

    In the top branch of the syntax:

    • c is any member of the user's character set. A single quotation mark (') within the literal must be preceded by an escape character. To represent one single quotation mark within a literal, enter two single quotation marks.
    • ' ' are two single quotation marks that begin and end text literals.

    In the bottom branch of the syntax:

    • Q or q indicates that the alternative quoting mechanism will be used. This mechanism allows a wide range of delimiters for the text string.
    • The outermost ' ' are two single quotation marks that precede and follow, respectively, the opening and closing quote_delimiter.
    • c is any member of the user's character set. You can include quotation marks (") in the text literal made up of c characters. You can also include the quote_delimiter, as long as it is not immediately followed by a single quotation mark.
    • quote_delimiter is any single- or multibyte character except space, tab, and return. The quote_delimiter can be a single quotation mark. However, if the quote_delimiter appears in the text literal itself, ensure that it is not immediately followed by a single quotation mark.
    • If the opening quote_delimiter is one of [, {, <, or (, then the closing quote_delimiter must be the corresponding ], }, >, or ). In all other cases, the opening and closing quote_delimiter must be the same character.

    Text literals have properties of both the CHAR and VARCHAR2 datatypes:

    • Within expressions and conditions, Oracle treats text literals as though they have the datatype CHAR by comparing them using blank-padded comparison semantics.
    • A text literal can have a maximum length of 4000 bytes.

    Here are some valid text literals:

    'Hello'
    'ORACLE.dbs'
    'Jackie''s raincoat'
    '09-MAR-98'
    N'nchar literal'

    Here are some valid text literals using the alternative quoting mechanism:

    q'!name LIKE '%DBMS_%%'!'
    q'<'So,' she said, 'It's finished.'>'
    q'{SELECT * FROM employees WHERE last_name = 'Smith';}'
    nq'ï Ÿ1234 ï'
    q'"name like '['"'

  • 相关阅读:
    【8-21】java学习笔记03
    【每天一点点】
    【8-20】java学习笔记02
    【8-19】java学习笔记01
    【8-18】JS学习01
    【8-17】HTML测试
    Selenium之WebDriverWait
    Selenium之XPATH定位方法
    Fiddler用法
    微服务架构系统的测试
  • 原文地址:https://www.cnblogs.com/minisculestep/p/4889939.html
Copyright © 2020-2023  润新知