QUOTENAME Function in SQL: should read

In this article you’ll learn QUOTENAME Function in SQL. This Function was first introduced in SQL Server 2012. Main use of this function is to add square brackets to the starting and ending of a string.

In many cases, this function is often used when generating SQL statements dynamically and where the table names, column names or other identifiers can include spaces and brackets. Let’s check out below section.

QUOTENAME Function in SQL: Syntax

QUOTENAME ('char_string' [ ,'quote_char'])

char_string” is the input string and is limited to 128 characters only. Second parameter is optional and if we do not pass “quote_char” then by default brackets are used.

Let’s check out its example as below:

Case1: Output will contain double quote at starting and ending of a string.

Select quotename('Rakesh Kumar','"')  as [Name]

Case2: Output will contain single quote at starting and ending of a string.

select quotename('Rakesh Kumar','''')  as [Name]

Case3: Output will contain square brackets at starting and ending of a string.

Select quotename('Rakesh Kumar')

As a first parameter it can accept hardcoded string, variable and table column name too. Like

declare  @test nvarchar(max)='Rakesh Kumar'
select quotename(@test,'')  as [Name] 
select quotename(FirstName,'')  as [Name] from  [user] 

I hope above explanation has given you deep understanding of QUOTENAME SQL function, and you will use this in your regular SQL development. For more info you can check Microsoft Docs

For more knowledge of MS SQL, you can check this section HERE

Leave a Reply

Your email address will not be published. Required fields are marked *