Avatar of milani_lucie
milani_lucie
Flag for United States of America asked on

QUOTENAME help needed - SQL Server

Hi,

I have the following command:

SELECT QUOTENAME('abc[]def')

It returns me

[abc[]]def]  -- Carefully look at the braces

There is a meaning if it returns me

[abc[]def]  -- Carefully look at the braces

Can you please explain to me why SQL Server is returning [abc[]]def] as result ?

Thanks
Microsoft SQL Server 2008Microsoft SQL Server 2005

Avatar of undefined
Last Comment
ralmada

8/22/2022 - Mon
SOLUTION
JestersGrind

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
ralmada

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Guy Hengel [angelIII / a3]

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ralmada


So basically let's say that you are creating a query dynamically
if you do

set @strSQL = 'select col1, col2, [abc[]def] from yourtable'

This will fail, because SQL will think that the column name is [abc[]. So to solve the problem you need to escape the ] character so that SQL understand that it's a literal character. The way of doing that is by repeating the character.

set @strSQL = 'select col1, col2, [abc[]]def] from yourtable' --this will be ok.
Now I wouldn't define any columns with any special characters such us the brackets. Just my two cents.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck