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')
QUOTENAME Function in SQL

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] 
or
select quotename(FirstName,'')  as [Name] from  [user] 
QUOTENAME Function in SQL

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 *

Explore More

How to enable SQL Server authentication

SQL Server Authentication is a default way to connect to MS SQL Server, but there may be many times that you need to use SQL Server Authentication to connect to

Set Transaction Isolation Level in SQL 

In this post we’ll discuss SQL server “Set Transaction Isolation Level”. These are of 5 types, and we’ll discuss all one by one below. READ UNCOMMITTED Read uncommitted specifies that

Is SQL NOLOCK bad practice?

In this post we’ll read about the use of [Nolock] in SQL Queries and stored procedures. Is this good or bad practice to use this in query. Let’s check out