How to Rename Something in SQL Server That Has Square Brackets in the Name

How to rename something in SQL Server that has square brackets in the name?

You do it the same way you do to create it:

exec sp_rename 'BookPublisher."[Book_Category]"', 'Book_Category', 'COLUMN';

Here's a little sample I made to test if this was even possible. At first I just assumed it was a misunderstanding of how [] can be used in SQL Server, turns out I was wrong, it is possible - you have to use double quotes to outside of the brackets.

begin tran

create table [Foo] ("[i]" int);

exec sp_help 'Foo';

exec sp_rename 'Foo."[i]"', 'i', 'column ';

exec sp_help 'Foo';

rollback tran

Renaming an index that has square brackets in the name

When you run this string through QUOTENAME it returns:

[[IX_ValidationCurationTimeStampUTC]]]

That is what you need to use!

EXEC sp_rename 'whatever.[[IX_ValidationCurationTimeStampUTC]]]', 'IX_ValidationCurationTimeStampUTC', 'INDEX'

What is the use of the square brackets [] in sql statements?

The brackets are required if you use keywords or special chars in the column names or identifiers. You could name a column [First Name] (with a space) – but then you'd need to use brackets every time you referred to that column.

The newer tools add them everywhere just in case or for consistency.

Mistake made while changing column name using sp_rename. (double colon created)

now you have to do this , so you put your column new which is "table.new_column" right now in double-quote or rectangular brackets like so :

exec sp_rename 'dbo.tablename."table.new_column"', 'new_column', 'COLUMN';

or

exec sp_rename 'dbo.tablename.[table.new_column]', 'new_column', 'COLUMN';

SQL Server having a column issue in renaming

Your column name uses a reserved keyword. If you don't like the square brackets, they're going to be required in a lot of scenarios, so maybe you should consider either (a) using a non-reserved word (like IsDefault) or (b) not using the designer. Maybe both.

Removing [] around column in SQL Server 2005

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.

What do brackets mean around column names in SQL Server?

Columns enclosed in square brackets are usually keywords or contain special characters or spaces.

What specific column name do you have enclosed in brackets?



Related Topics



Leave a reply



Submit