I have a SQL Server database employeedb
and table employee
with column EmployeeID
whose value I need to update and simultaneously rename that column.
EmployeeID
-----------
83456785647
I need to add a '-'
2 digit places from the end and rename EmployeeID
to EmployeeNr
to get the below output
EmployeeNr
------------
834567856-47
I can achieve the desired outcome in step wise by running two separate SQL statements as:
update employee
set EmployeeID = substring(cast(EmployeeID as varchar(255)), 1, len(cast(EmployeeID as varchar(255)))-2) + '-' + right(cast(EmployeeID as varchar(255)), 2)
and to change the column name I execute a separate query as:
USE employeedb;
GO
EXEC sp_rename 'employee.EmployeeID', 'EmployeeNr', 'COLUMN';
GO
Is there a way to combine both statements in a single SQL statement?