Open In App

QUOTENAME() Function in SQL Server

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 :



Here, the string is limited to 128.

Syntax :



QUOTENAME(string, quote_char)

Parameter :
This method accepts two parameter as given below :

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.

Article Tags :
SQL