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.
2 Likes

More info here:

1 Like

I used this after looking through a ton of stack answers

EXEC sp_rename 'TableName.OldName', 'NewName', 'COLUMN'
2 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

1 Like

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