SQL Server QUOTENAME() Function
The QUOTENAME()
function in SQL Server adds delimiters (like brackets or quotes) to a string, making it a valid SQL identifier. This is crucial for handling strings that might contain spaces or special characters that aren't allowed in standard SQL identifiers.
QUOTENAME(): Definition and Usage
SQL Server identifiers (like table and column names) have specific rules. They cannot contain spaces or certain special characters. QUOTENAME()
helps you safely create valid identifiers from potentially problematic strings by "escaping" them with delimiters.
Syntax
Syntax
QUOTENAME(string, quote_char)
Parameter Values
Parameter | Description |
---|---|
string |
The input string (Unicode character data, up to 128 characters) that needs to be converted into a valid delimited identifier. This is required. |
quote_char (Optional) |
A single character to use as the delimiter. If omitted, square brackets ([] ) are used by default. Possible values: ' , [ , ] , " , ( , ) , < , > , { , } , ` |
Examples
Default Delimiters (Brackets)
This example uses the default square bracket delimiters.
Syntax
SELECT QUOTENAME('abcdef');
Output
[abcdef]
Custom Delimiters (Parentheses)
This example uses parentheses as delimiters.
Syntax
SELECT QUOTENAME('abcdef', '()');
Output
(abcdef)
Technical Details
The QUOTENAME()
function is available in:
- SQL Server (starting with version 2008)
- Azure SQL Database
- Azure SQL Data Warehouse
- Parallel Data Warehouse