How to change a column name in SQL?

ALTER TABLE mytable
RENAME COLUMN "oldName" TO "newName";

errors out

Someone named a column Date, and it is giving me a bunch of issues.

1 Like

That looks about right. But check your syntax against the documentation for your brand of DB.
Also, you might not have permission. What is the actual error?

1 Like

Incorrect syntax near ā€˜RENAMEā€™.

I am using Microsoft SQL, SQL Bridge module.

I havenā€™t found keyword ā€˜renameā€™ in the W3schools SQL section.
In alter table, they donā€™t show changing the name of a column that I saw

Rename column name in MS SQL Server
The process of renaming column name is MS SQL Server is different when compared to the other databases. In MS SQL Server, you have to use the stored procedure called sp_rename.

Syntax
1
sp_rename 'TableName.OldColumnName', 'New ColumnName', 'COLUMN';
Example:
Write a query to rename the column name ā€œBIDā€ to ā€œBooksIDā€.

1
sp_rename 'Books.BID', 'BooksID', 'COLUMN';
The resulting output will be the same as that for the above queries. Now, that you have understood how to rename a column name in various databases, let us see how you can rename a table name.
3 Likes

More info here:

1 Like

I used this after looking through a ton of stack answers

EXEC sp_rename 'TableName.OldName', 'NewName', 'COLUMN'
4 Likes

It was probable that the column name of ā€œDateā€ was upsetting the process (as Date is a type). So a fully qualified name would work

2 Likes

Might also be worth adding code to make sure problematic names canā€™t be used.

To change a column name in SQL, you can use the following syntax:
ALTER TABLE mytable
RENAME COLUMN oldName TO newName;

Make sure to replace mytable , oldName , and newName with the appropriate table name, old column name, and new column name respectively.

For detailed instructions on how to rename a column in SQL, you can refer to this blog: How to rename a column in SQL. It provides step-by-step instructions and additional insights into renaming columns in SQL

Gives me an error like incorrect syntax near rename.
Something is wrong where Rename is not recognized at all as a keyword.

I don't understand why Rename keyword was not recognized.
I see it used all over and wonder what I did wrong with it.

You're using SQL server. It's not MySQL.
Make sure you're searching for the db you're using, otherwise you'll end up having this problem a lot.

tip: SQL server's language is Transact SQL. Add that or t-sql in your googling.

Second link when searching "sql server rename column":

The first link is a stackoverflow answer with the exact same procedure call.

@Tanu_Rai You're answering a 2 years old post, and your answer is exactly the code that's in the very first post, tried by the op and that didn't work. It's nice of you to answer, but please pay attention to what you're answering to.

2 Likes