Search code examples
sql-servert-sql

QUOTENAME() with parenthesis, curly brackets, angle brackets, or grave accents


While reading the documentation for QUOTENAME(), I see that it supports a variety of delimiter characters:

  • Square brackets - [] (default)
  • Parenthesis - ()
  • Curly brackets - {}
  • Angle brackets - <>
  • Single quotes - '
  • Double quotes - "
  • Grave accents - `

In addition to wrapping the content in opening and closing delimiters, QUOTENAME() appears to double up any contained characters that match the closing delimiter. For example: QUOTENAME('matrix[2][3]') yields [matrix[2]][3]]] and QUOTENAME('cell(2)(3)', '(') yields (cell(2))(3))).

While I understand the usage of single quotes, double quotes, and square brackets to delimit identifiers and text values (primarily when constructing dynamic SQL), I am not sure what use the other forms might have.

Are there any places in SQL Server syntax where parenthesis, curly bracket, angle bracket, or grave accent delimited content has any special meaning? Are there any languages (XML, JSON, etc.) outside of SQL where those other forms might be useful? Such cases would have to recognize the doubled character as an escape.

This is more a question of interest rather than a particular problem that I am trying to solve.


Solution

  • From the discussion, it appears that these other options were long-present undocumented behaviors that seem to have little or no real-world utility.

    Other languages or file formats that use some of these characters as quotes, do not escape embedded delimiters in the manner that QUOTENAME() does. (Backslash escaping seems to be the dominant method in these.)

    When the documentation became open source and community maintained, these undocumented behaviors were added to the documentation. It might have been better to leave undocumented behavior as undocumented or perhaps as explicitly undefined.