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
"You Tried to Execute a Query That Does Not Include the Specified Aggregate Function"
SQL Comma-Separated Row with Group by Clause
How to Store a List in a Db Column
What Are the Principles Behind, and Benefits Of, the "Party Model"
SQL Server 2008 Paging Methods
SQL Server (Tsql) - How to Exec Statements in Parallel
Why Using a Udf in a SQL Query Leads to Cartesian Product
Is of a Type That Is Invalid for Use as a Key Column in an Index
How to Concatenate Numbers and Strings to Format Numbers in T-Sql
Split String and Take Last Element
Regular Expression to Match Common SQL Syntax
Why Do Null Values Come First When Ordering Desc in a Postgresql Query
What Are Projection and Selection
Given an Rgb Value What Would Be the Best Way to Find the Closest Match in the Database
Cte Error: "Types Don't Match Between the Anchor and the Recursive Part"
Query Runs Slow with Date Expression, But Fast with String Literal
SQL Where Joined Set Must Contain All Values But May Contain More