Rename Column in SQL Server
Last Updated :
27 May, 2024
SQL Server is a widely used Relational Database Management System (RDBMS) that allows users to create and manage databases effectively. Renaming a column in a database is a common task usually required when users want to change the database schema. In this article, we will explore different methods to rename columns in SQL Server.
Rename Column in SQL Server
There are 2 methods through which we can rename columns of a database in SQL Server which are explained below:
- Using the sp_rename system stored procedure
- Using ALTER statement
Let's set up an environment
To understand how we can rename columns in SQL Server, we will consider the table Customers as shown below:
CustomerID
| CustomerName
| City
| State
| Age
|
---|
1
| Amit Kumar
| Mumbai
| Maharashtra
| 28
|
2
| Kavya Sharma
| Delhi
| Delhi
| 35
|
3
| Amit Singh
| Bangalore
| Karnataka
| 42
|
4
| Rohan Kumar
| Kolkata
| West Bengal
|
33
|
1. Using the sp_rename System Stored Procedure
To rename a column of a database in SQL Server, we can use the sp_rename system stored procedure. The sp_rename procedure is a built-in system stored procedure that allows the users to rename various database objects like tables, columns, views, and indexes. Following is the syntax to use the sp_rename system stored procedure:
Syntax:
EXEC sp_rename 'table_name.old_column_name', 'new_column_name', 'COLUMN'
where:
- table_name: is the name of the table containing the column you want to rename.
- old_column_name: is the current name of the column.
- new_column_name: is the desired new name for the column.
- COLUMN: is an additional parameter that directs the system procedure to rename the column.
Example
To change the column name from State to Residential State of the above table Customers we will have to run the following query
Query
EXEC sp_rename 'Customers.State, 'Residential State', 'COLUMN'
Output:
CustomerID
| CustomerName
| City
| Residential State
| Age
|
---|
1
| Amit Kumar
| Mumbai
| Maharashtra
| 28
|
2
| Kavya Sharma
| Delhi
| Delhi
| 35
|
3
| Amit Singh
| Bangalore
| Karnataka
| 42
|
4
| Rohan Kumar
| Kolkata
| West Bengal
|
33
|
Explanation: The EXEC sp_rename command renames the State column in the Customers table to 'Residential State'. The resulting table schema reflects this change, displaying the new column name 'Residential State' instead of State, with all data remaining intact.
2. Using ALTER Statement
To rename the column of a database in SQL Server, we can also use the ALTER table command. ALTER is a Data Definition Language(DDL) command that is used to update the schema of tables, views, and indexes in SQL Server. Following is the syntax to rename the column in SQL Server using the ALTER command:
Syntax:
ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name
where:
- table_name: is the name of the table containing the column you want to rename.
- old_column_name: is the current name of the column that you want to rename.
- new_column_name: is the desired new name for the column.
Example
To change the column name from CustomerName to User of the above table Customers we will have to run the following query
Query:
ALTER TABLE Customers RENAME COLUMN CustomerName TO Users
Output:
CustomerID
| Users
| City
| State
| Age
|
---|
1
| Amit Kumar
| Mumbai
| Maharashtra
| 28
|
2
| Kavya Sharma
| Delhi
| Delhi
| 35
|
3
| Amit Singh
| Bangalore
| Karnataka
| 42
|
4
| Rohan Kumar
| Kolkata
| West Bengal
|
33
|
Explanation: The 'ALTER TABLE Customers RENAME COLUMN CustomerName TO Users' command attempts to rename the CustomerName column to Users. However, SQL Server does not support this syntax; use sp_rename instead for renaming columns in SQL Server.
Conclusion
In conclusion, SQL Server provides efficient tools for renaming columns, primarily using the sp_rename system stored procedure. While ALTER TABLE RENAME COLUMN is a standard SQL syntax in some database systems, SQL Server relies on sp_rename for this operation. Users can effectively manage and update their database schemas using these methods.
Similar Reads
Rename column SQL Server 2008
Renaming a column in a database is a common task that often arises when users want to change the database schema. In SQL Server 2008, renaming columns can be done efficiently using the sp_rename system-stored procedure, as the ALTER TABLE RENAME COLUMN syntax is not supported in this version. This a
4 min read
Deleting a Column in SQL Server
Structure Query Language (SQL) is a standard language to manipulate and manage the database. SQL is a very powerful language for managing the database. SQL Server Delete command is one of the SQL commands that is used to remove the data that is not useful or due to which inconsistency occurred in th
5 min read
SQL Server REPLACE() Function
SQL Server is a strong relational database management system (RDBMS) developed to manage large data efficiently. In SQL Server, the REPLACE() function is used to modify or replace a substring within a given string. Taking about the real word uses, the REPLACE() function is vastly used in data proces
4 min read
REVERSE() Function in SQL Server
The REVERSE() function in SQL Server is a simple and powerful tool designed to reverse the order of characters in a string. By taking a string input, it returns a new string with its characters arranged in the opposite sequence. In this article, We will learn to REVERSE() Functions in SQL Server by
3 min read
SQLite Rename Column
Renaming a Column becomes necessary when there is a certain change that appears to be visible in the column the name given to the column previously is vague or it doesn't represent what the column holds exactly. SQLite provides the modified version of the ALTER TABLE command which lets the user rena
6 min read
How to Rename a View in SQL Server?
The view is a virtual table based on the result set of an SQL statement. It is like the subset of the table and created to optimize the database experience. Like a real table, this also contains rows and columns. The data in a view are extracted from one or more real tables in the database. Renaming
2 min read
sp_columns - SQL Server
In SQL Server, managing and understanding database schemas is crucial for effective database administration and development. The sp_columns stored procedure is a valuable tool for retrieving detailed metadata about the columns of a specified table or view. In this article, We will learn about sp_col
6 min read
Change Primary Key Column in SQL Server
Primary Key refers to the column of a table that uniquely identifies a row in a table. It contains unique values and can not contain NULL values. For the purpose of the demonstration, we will be creating geeks for geeks table in a database called âgeeksâ. Step 1: Creating the database The database i
2 min read
How to Rename Columns in Tidyverse
Renaming columns is an important step in data processing since it allows for easier interpretation and analysis. Within the field of data research, the Tidyverse package provides extensive capabilities for this goal, including quick ways for renaming columns smoothly. What is Tidyverse?Tidyverse is
3 min read
How to Search For Column Names in SQL?
In SQL, sometimes we need to search the column names in a table using the prefixes. For this article, we will be using the Microsoft SQL Server as our database and Select keyword. Step 1: Create a Database. For this use the below command to create a database named GeeksForGeeks. Query: CREATE DATABA
2 min read