SQL - QUOTENAME() Function



The SQL QUOTENAME() function is used to retrieve a Unicode string with delimiters. It accepts two parameters char_str, and quote_char, and returns a string with a delimiter added, in order to make the String a valid SQL delimiter identifier.

A delimiter is a sequence of one or more characters specifying the boundary between separate, independent regions in the plain text, character strings, words, or any other data items. The character string limit is 128 characters, and if, its length is greater than 128 characters, it returns a NULL value.

Note − The default delimiter is left([) and right(]) brackets.

Syntax

Following is the syntax of the SQL QUOTENAME() function −

QUOTENAME(char_string, quote_character)

Parameters

  • char_string − It is a string of Unicode character data.

  • quote_character − It is a single-character string to use as the delimiter.

Return value

This function returns a Unicode string with the delimiters.

Example

In the following example,we are using the SQL QUOTENAME() function to retrieve a Unicode string with the delimiters of the current characters string ‘tutorials[]point’.

SELECT QUOTENAME('tutorials[]point') AS UNICODE_STRING;

Output

On executing the above program, it will produce the following output −

+-----------------------+
| UNICODE_STRING        |
+-----------------------+
| [tutorials[]]point]   |
+-----------------------+

Example

Following is another example of the SQL QUOTENAME() function. In this example, we are trying to retrieve the Unicode string with a delimiter of the current characters string ‘hello world’, which initially does not contain any delimiter.

SELECT QUOTENAME('hello world') AS UNICODE_STRING;

Output

The above SQL query generates the following output −

+------------------+
| UNICODE_STRING   |
+------------------+
| [hello world]    |
+------------------+

Example

If we pass the argument quote_char(custom delimiter) value to the QUOTENAME() function, this function returns the Unicode string with a specified delimiter.

In the following example,we are using the SQL QUOTENAME() function to retrieve the Unicode string with the specified delimiter ‘{}’ of the current character string ‘Tutorials Point’.

SELECT QUOTENAME('Tutorials Point', '{}') AS UNICODE_STRING;

Output

Following is the output of the above query −

+----------------------------+
| UNICODE_STRING             |
+----------------------------+
| {Tutorials Point}          |
+----------------------------+

Example

You can also pass the table column as an argument to the SQL QUOTENAME() function to get the Unicode string with the specified delimiter. Assume we have created a table with the name Customers using the CREATE statement as follows −

CREATE TABLE CUSTOMERS(    
   ID INT NOT NULL,    
   NAME VARCHAR (20) NOT NULL,    
   AGE INT NOT NULL,    
   ADDRESS CHAR (25) ,    
   SALARY DECIMAL (18, 2));

Now, let's insert some records into the Customers table using the INSERT statement as follows −

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 ); 
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (2, 'Khilan', 25, 'Delhi', 1500.00 ); 
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (3, 'kaushik', 23, 'Kota', 2000.00 ); 
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (4, 'Chaitali', 25, 'Mumbai', 6500.00 );

The following SQL query retrieves the Unicode string value with the specified delimiter ‘()’ of the column Name in the Customers table −

SELECT ID, NAME, QUOTENAME(NAME, '()') AS UNICODE_STRING FROM CUSTOMERS;

Output

Following is the output of the above query −

+----+----------+----------------+ 
| ID | NAME     | UNICODE_STRING |  
+----+----------+----------------+ 
|  1 | Ramesh   | (Ramesh)       |  
|  2 | Khilan   | (Khilan)       | 
|  3 | kaushik  | (kaushik)      |  
|  4 | Chaitali | (Chaitali)     |  
+----+----------+----------------+
sql-string-functions.htm
Advertisements