Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing [] around column in SQL Server 2005

when I was renaming the column in SQL Server, I accidentally inserted the square brackets around the column. The actual statement that I used was:

SP_RENAME 'customer.[EMPLOYEENAMES]', '[EMPLOYEENAME]', 'COLUMN' 

But when I try to retrieve the data it just says and I even tried with out square brackets, it gives the same error

Invalid column name '[EMPLOYEENAME]'.

How should I remove the square brackets.

like image 481
Sree Avatar asked Apr 13 '12 07:04

Sree


People also ask

How do I get rid of square brackets in SQL?

To remove the square brackets that surround the JSON output of the FOR JSON clause by default, specify the WITHOUT_ARRAY_WRAPPER option.

What does [] mean in SQL Server?

the brackets are special characters in sql server that are used to explicitly delimit information. they can be used in xml as per the article, they can also be used to specify meta names (column, table, etc.)

How do you escape a bracket in SQL?

We can escape square brackets using two methods: Escape using one more square bracket. Escape using Escape character.

How do I strip a column in SQL?

Right-click the column you want to delete and choose Delete Column from the shortcut menu. If the column participates in a relationship (FOREIGN KEY or PRIMARY KEY), a message prompts you to confirm the deletion of the selected columns and their relationships. Choose Yes.


2 Answers

This will restore order in your database:

EXEC SP_RENAME 'customer."[EmployeeName]"', 'EmployeeName','COLUMN' 

You cannot use double brackets because it returns syntax error. Quotes circumvent this limitation.

like image 145
Nikola Markovinović Avatar answered Sep 23 '22 13:09

Nikola Markovinović


As you've now got a column with square brackets in the name itself, you can access that column by:

SELECT [[EmployeeName]]]  FROM Customer 

Yes, all those extra square brackets are a bit unwieldy :)

So, I'd rename your column again to remove the brackets:

EXEC SP_RENAME 'customer.[[EmployeeName]]]', 'EmployeeName','COLUMN' 

So you can then reference "normally":

SELECT EmployeeName FROM Customer 
like image 26
AdaTheDev Avatar answered Sep 22 '22 13:09

AdaTheDev