Renaming a column in SQL Server

Although renaming a column is extremely uncommon, SQL Server provided us with the ability to do so in the event that it was required. I came to the realization that I needed to rename a column while working on one of the blog pieces. I figured I’d offer this information so that newcomers can learn from it.

CREATE TABLE tbStudentDetails(
StudentID INT,
StudentName VARCHAR(30),
Ranking INT);

INSERT INTO tbStudentDetails VALUES
(1001,’Zafar Iqbal’,2),
(3,’Tahir Iqbal’,1),
(5,’Danial Hussain’,3)

— Before Column Rename
SELECT * FROM tbStudentDetails

— Renaming the existing column
EXEC sp_rename ‘tbStudentDetails.Ranking’, ‘GradeID’, ‘COLUMN’

— After Column Rename
SELECT * FROM tbStudentDetails

RenameColumnSQLServer

Hope you find this article helpful.

Please subscribe for more interesting updates.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s