Open In App

QUOTENAME() Function in SQL Server

Improve
Improve
Like Article
Like
Save
Share
Report

QUOTENAME() function :
This function in SQL Server is used to return a Unicode string with delimiters added in order to make the string a valid SQL Server delimited identifier.

Features :

  • This function is used to find a Unicode string with delimiters added.
  • This function accepts only strings and delimiters.
  • This function add delimiters by default if not specified.
  • This function always returns string along with delimiters.

Here, the string is limited to 128.

Syntax :

QUOTENAME(string, quote_char)

Parameter :
This method accepts two parameter as given below :

  • string: Specified string of Unicode character data which is limited to 128 characters.
  • quote_char : It is optional. It is a one-character string which is to be used as the delimiter. For example, it can be a single quotation mark i.e, ( ‘ ), or a left or right bracket i.e, ( [] ), or a double quotation mark i.e, ( ” ), or a left or right parenthesis i.e, ( () ), or a greater than or less than sign i.e, ( >< ), or a left or right brace i.e, ( {} ) or a backtick i.e, ( ` ). Moreover, if this parameter is not specified, the brackets are used by default.

Returns :
It returns a Unicode string with delimiters added in order to make the string a valid SQL Server delimited identifier.

Example-1 :
Getting the Unicode string of the string “xyz”.

SELECT QUOTENAME('xyz');

Output :

[xyz]

Here, the quote_char parameter is not defined but brackets are added to the output by default.

Example-2 :
Getting the Unicode string with parenthesis delimiters.

SELECT QUOTENAME('abc', '{}');

Output :

{abc}

Here, the delimiters are specified in the parameters so they are returned as output.

Example-3 :
Using QUOTENAME() function with a variable and getting the Unicode string of the specified string.

DECLARE @string VARCHAR(3);  
SET @string = '123';  
SELECT QUOTENAME(@string);

Output :

[123]

Example-4 :
Using QUOTENAME() function with a variable and getting the Unicode string of the specified string as well as delimiters.

DECLARE @string VARCHAR(4);
DECLARE @delimiter VARCHAR(2);
SET @string = 'jk12';
SET @delimiter = '()';
SELECT QUOTENAME(@string, @delimiter);

Output :

(jk12)

Example-5 :
Getting the Unicode string with greater than sign.

SELECT QUOTENAME('23', '>');

Output :

<23>

This delimiter only works with numbers.

Application :
This function is used to return the Unicode string with delimiters added to it in order to make the string a valid SQL Server delimited identifier.


Last Updated : 30 Dec, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads