Thursday 12 September 2013

QUOTENAME :

Quotename() is a SQL Server String function. It Returns a Unicode string with the delimiters added to make the input string a valid SQL Server delimited identifier.

Syntax :

QUOTENAME ( 'character_string' [ , 'quote_character' ] ) 
If the character_string or column value is greater than the 128 characters then quotename function will returns NULL.
Example`s :
SELECT Quotename('satya') 
Result : [satya]
Note : If quote_character is not specified, by default it take brackets.
SELECT Quotename('satya', '(') 
Result : (satya)

SELECT Quotename('satya', '{') 
Result : {satya}

SELECT Quotename('satya', '[') 
Result : [satya]

SELECT Quotename('satya', '''') 
Result : 'satya'

SELECT Quotename('satya', '"') 
Result : "satya"

SELECT Quotename('satya', '<') 
Result : <satya>

Applying QUOTENAME Function to Column :

CREATE TABLE table_quotename 
  ( 
     info VARCHAR(250) 
  ) 

INSERT INTO table_quotename 
SELECT Replicate('satya mssql', 2) 
UNION ALL 
SELECT Replicate('satya mssql', 4) 
UNION ALL 
SELECT Replicate('satya mssql', 13) 

SELECT Quotename(info), 
       Quotename(info, '['), 
       Quotename(info, '{'), 
       Quotename(info, ''''), 
       Quotename(info, '(') 
FROM   table_quotename 
In the above query result last column values are showing as NULL, because values in the third row is greater than 128 characters that`s why result is NULL.