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.
To remove the square brackets that surround the JSON output of the FOR JSON clause by default, specify the WITHOUT_ARRAY_WRAPPER option.
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.)
We can escape square brackets using two methods: Escape using one more square bracket. Escape using Escape character.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With