{"id":5280,"date":"2013-04-15T00:00:19","date_gmt":"2013-04-15T00:00:19","guid":{"rendered":"http:\/\/craftydba.com\/?p=5280"},"modified":"2017-10-11T16:10:19","modified_gmt":"2017-10-11T16:10:19","slug":"string-functions-quotename","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=5280","title":{"rendered":"String Functions &#8211; QUOTENAME()"},"content":{"rendered":"<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/04\/turquoise-yarn-md.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/04\/turquoise-yarn-md-150x150.png\" alt=\"\" title=\"turquoise-yarn-md\" width=\"150\" height=\"150\" class=\"alignleft size-thumbnail wp-image-5158\" \/><\/a><br \/>\nI am going to continue my series of very short articles or tidbits on Transaction SQL string functions.  I will exploring the QUOTENAME() function today.<\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms176114.aspx\">QUOTENAME()<\/a> function takes a input of type <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms191240(v=sql.105).aspx\">sysname<\/a>, 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.  <\/p>\n<p>The following example creates a table name in the adventure works database from two variables.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"string functions - quotename()\">\r\n-- Set local variables\r\ndeclare @var_schema sysname = 'SalesLT';\r\ndeclare @var_name sysname = 'Product';\r\n\r\n-- Make table name from ADW\r\nselect quotename(@var_schema) + '.' + quotename(@var_name) as my_name;\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput:\r\n\r\nmy_name\r\n-------------------\r\n[SalesLT].[Product]\r\n<\/pre>\n<\/p>\n<p>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.  <\/p>\n<p>However, the exclamation point is not considered a valid quote character.  A invalid quote character converts the output string to NULL.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"string functions - quotename()\">\r\n-- Change quote character (valid character)\r\nselect quotename('AdventureWorks', '\"') as my_name;\r\n\r\n-- Change quote character (invalid character)\r\nselect quotename('AdventureWorks', '!') as my_name;\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput:\r\n\r\nmy_name\r\n---------------\r\n\"AdventureWorks\"\r\n\r\nmy_name\r\n---------------\r\nNULL\r\n<\/pre>\n<\/p>\n<p>One thing to always worry about when using TSQL functions is how will it react with a EMPTY strings or NULL values? <\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"string functions - quotename()\">\r\n-- Empty string returns brackets\r\nselect quotename('') as my_name;\r\n\r\n-- Null returns Null\r\nselect quotename(NULL) as my_name;\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\n\r\noutput:\r\n\r\nmy_name\r\n---------------\r\n[]\r\n\r\nmy_name\r\n---------------\r\nNULL\r\n<\/pre>\n<\/p>\n<p>Next time, I will be exploring the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms186862.aspx\">REPLACE()<\/a> function.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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. &#8212; Set local variables declare @var_schema sysname = &#8216;SalesLT&#8217;; declare @var_name&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[814],"tags":[31,15,824,815,29],"class_list":["post-5280","post","type-post","status-publish","format-standard","hentry","category-very-short-articles","tag-database-developer","tag-john-f-miner-iii","tag-quotename","tag-string-function","tag-tsql"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5280","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5280"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5280\/revisions"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}