What is the difference between square brackets and single quotes for aliasing in SQL Server?

后端 未结 4 2126
花落未央
花落未央 2020-12-28 15:15

I have seen some people alias column names using single quotes eg:

select orderID \'Order No\' from orders

and others use square brackets e

4条回答
  •  粉色の甜心
    2020-12-28 15:47

    It depends on what settings you have in force whether 's are valid or not. And you missed out ". See Delimited Identifiers:

    When QUOTED_IDENTIFIER is set to ON, SQL Server follows the ISO rules for the use of double quotation marks (")and the single quotation mark (') in SQL statements. For example:

    • Double quotation marks can be used only to delimit identifiers. They cannot be used to delimit character strings.

    • Single quotation marks must be used to enclose character strings. They cannot be used to delimit identifiers.


    When QUOTED_IDENTIFIER is set to OFF, SQL Server uses the following rules for single and double quotation marks:

    • Quotation marks cannot be used to delimit identifiers. Instead, brackets have to be used as delimiters.

    • Single or double quotation marks can be used to enclose character strings.

    And finally:

    Delimiters in brackets can always be used, regardless of the setting of QUOTED_IDENTIFIER

    Where, in all of the above quotes, when they refer to brackets they're talking about [] brackets.

提交回复
热议问题