Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 ADD COLUMN problem

Author  Topic 

funk.phenomena
Posting Yak Master

121 Posts

Posted - 2012-07-09 : 17:36:21
Hi All - I'm trying to add a column name which already includes square brackets in the column name. The actual column name is '[1] Question 1'.

It's not letting me. Any ideas?

[CODE]
ALTER TABLE Table1
ADD [[1] Question 1] NVARCHAR(MAX)
GO
[/CODE]

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-07-09 : 17:40:02
Why oh why would you want to do this? This will make coding very difficult. Just use Question1 or similar for the name, and then in your application you can display the name differently.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

funk.phenomena
Posting Yak Master

121 Posts

Posted - 2012-07-09 : 17:43:51
Hi Tara - This is the way the table was previously coded. the table is linked to SSRS flows, and would take too much work to change the column names.

Any idea on how I can add column as described above? Thanks!
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-07-09 : 18:00:16
Use double quotes to escape.
ALTER TABLE Table1
ADD "[1] Question 1" NVARCHAR(MAX)
GO
If you need to use double quotes in your column name, use square brackets as escape.

Now, if you want to use both square brackets AND double quotes in your table name, what should one do? (Left as an exercise to the reader so s/he can torture himself/herself).
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-07-09 : 18:04:59
I think it was Rob Volk who had a SQL Saturday presentation on how to torture yourself (or others) using imaginative column names. I think the following gems are from that:
CREATE TABLE #tmp("[id]x" INT);
CREATE TABLE #tmp2 ("[idx]]" INT, ["abcd"] INT)
CREATE TABLE #tmp3 (" " INT);
CREATE TABLE #tmp4 ([[abcd[] INT);
CREATE TABLE #tmp5 ([[[[] INT);
CREATE TABLE #tmp6 ("[[""]]""" INT);
(Rob, if it was not you, my apologies).
Go to Top of Page
   

- Advertisement -