SQL Server: QUOTENAME Function

The QUOTENAME() function in SQL Server adds delimiters to database object names, including tables, columns, schemas, and databases.

Syntax:

QUOTENAME ( 'character_string' [ , 'quote_character' ] )

character_string: The name of the object or the text that you want to delimit.
quote_character: The type of delimiter character (optional).

If quote_character is not specified, SQL Server defaults to square brackets [ ]. However, you can also use other characters, such as: [, ", ', (, {, ), }, <, >, and ].

E.g.:

SELECT
    QUOTENAME('Payment') AS DefaultDelimiter,
    QUOTENAME('Payment', '"') AS DoubleQuote,
    QUOTENAME('Payment', '''') AS SingleQuote,
    QUOTENAME('Payment', '(') AS Parentheses



Important things to keep in mind:
  • 128-character limit: The maximum length for the input is 128 characters (sysname).
  • Returns NULL if too long: If the input goes over 128 characters, the function will return NULL.
  • Built for identifiers, not data: It is meant for protecting object identifiers, not for securing actual data values.
  • Handle schemas separately: If you are using a table name with a schema, you should delimit the schema and the table name individually.

Post a Comment

0 Comments