How to Rename Column Name in Sql Server, or How to rename Table Name is one of the most Frequent Question in SQL Server forums.
How to Rename Column Labels? Hi, How do I rename the column labels in the table? For Instance, if I have a table like this, and I want to have the column labels changed from 'A1 A2 A3 A4 A5' to.
![]()
In this article we will show you, Rename Table Name and Column Name in SQL Server with example.
To demonstrate the SQL rename table name and column name, We are going to use the Employees Details table present in our [SQL Test] Database. And the data present in the Employee Details Table is:
Rename Table Name and Column Name in SQL Server
In this series of examples we are going to use the standard stored procedure SP_RENAME to rename SQL Server Table name and column name.
SQL Server Rename Table Name
In this SQL Rename table name example we will rename the table name using the SP_RENAME. Syntax behind this approach is as shown below:
We are going to use above specified syntax to rename the EmployeesDetails table to NewEmployeesDetails
SQL CODE
OUTPUT
Now let me try to select records from old table
As you can see from the above screenshot, it is throwing an error. Let me try with New table name
SQL Server Rename Column Name
In this SQL rename column name example we will rename column name using the SP_RENAME, syntax behind this approach is as shown below:
We are going to use above specified syntax to rename the old column name YearlyIncome to new name: Annual Income
SQL CODE
OUTPUT
From the below screenshot you can see the changed column
Thank You for Visiting Our Blog
Modify names by name, not position.
Modify names by name, not position.
UsageArguments
Aliases![]()
Exampleslibrary(plyr) x <- c('a' = 1, 'b' = 2, d = 3, 4)# Rename column d to 'c', updating the variable 'x' with the resultx <- rename(x, replace = c('d' = 'c'))x# Rename column 'disp' to 'displacement'rename(mtcars, c('disp' = 'displacement'))
Community examples
```{r}library(tidyr)library(dplyr)Iris <- irisselect(Iris, everything()) # base lineselect(Iris, New_Name = Old_Name) # Templateselect(Iris, petal_length = Petal.Length) # Worksselect(Iris, `petal length` = Petal.Length) # Worksselect(Iris, 'petal length' = Petal.Length) # Worksselect(Iris, 'petal length' = Petal.Length) # Worksrename(Iris, New_Name = Old_Name) # Templaterename(Iris, petal_length = Petal.Length) %>% head() # Worksrename(Iris, 'petal length' = Petal.Length) %>% head() # Worksrename(Iris, 'petal length' = Petal.Length) %>% head() # Worksrename(Iris, `petal length` = Petal.Length) %>% head() # Works```
[email protected] at Dec 22, 2018 plyr v1.8.4
## Rename column names which originally have spaces in themThe rename function takes the replace argument as: replace = c(new_name = old_name)```r library(tidyr) library(dplyr) degree = c('Biology','Medicine') salary = c(100000, 150000) df = cbind(degree, salary) df = as.data.frame(df) df # name the columns with spaces names(df) = c('Undergraduate Major', 'Starting Salary') # view the new names - these contain spaces now names(df) # view the data frame before the renaming # as you can see column names contain spaces df # rename (new_name1 = `old name1`, new_name2 = `old name2`) # Note: the new name comes first, followed by =, followed by the old name # i.e. College.Major is what I want to name the column, `Undergraduate Major` is the old name df_clean <- df %>% rename(College.Major=`Undergraduate Major`, Starting.Salary=`Starting Salary`) names(df_clean) df_clean```
API documentation
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |