Wednesday, 26 December 2018

Quotename function in SQL server

Quotename () is a SQL Server String function. Quotename function adds the square brackets ([]) or ‘’ (single quote) or “” (double quote) to the beginning and end of the string. This function is helpful when database object names have space between them and we are creating the dynamic SQL. If there is any space in SQL object and without [] square bracket then it will throw an error. Quotename () function can take string up to 128 characters as input.
Syntax of Quotename
QUOTENAME (character_string’, 'Quote_character’) –Quote_character may be [ or ] or ‘’ or “”.
QUOTENAME (‘character_string')
 By default Quote character is [].

Let’s the example

Creating a table
create table [Employee details]
( id int identity(1,1),
name varchar(100)
)
insert into [Employee details] values ('Bagesh Kumar Singh')
insert into [Employee details] values ('Rajesh Kumar Singh')
insert into [Employee details] values ('Mahesh Kumar Singh')
insert into [Employee details] values ('Ganesh Kumar Singh')
Now I am creating a dynamic SQL statement.
   
Either we need to write the [] in the table name like
   
Or we need to use Quotename function as below
  
It prepares like above.
  
If we want a single quote or double quote in the name we will use Quotename function.
  
We will use other than [],’’,”” it will return a null value. See below.
  

No comments:

Post a Comment

If you have any doubt, please let me know.

Popular Posts