String Functions – QUOTENAME()


I am going to continue my series of very short articles or tidbits on Transaction SQL string functions. I will exploring the QUOTENAME() function today.

The QUOTENAME() function takes a input of type sysname, nvarchar(128) and returns a string, nvarchar(258) with quote characters at front and end of the string. A optional string can be specified to redefine the default quote character for TSQL.

The following example creates a table name in the adventure works database from two variables.

The default quote characters [] can be changed by supplying a valid value. It is undocumented what is valid in Books Online. I did notice that {}, (), and <> work via some testing on my own.

However, the exclamation point is not considered a valid quote character. A invalid quote character converts the output string to NULL.

One thing to always worry about when using TSQL functions is how will it react with a EMPTY strings or NULL values?

Next time, I will be exploring the REPLACE() function.

Related posts

Leave a Comment