Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL REPLACE statement to replace some numbers not all

I am trying to use a T-SQL REPLACE statement to replace the middle 4 numbers under the DepartmentID column....

I understand the simple way of changing one set of DepartmentID numbers I want to be able to change all middle numbers under DepartmentID

SELECT REPLACE (DepartmentID,'000701280918','000799990918') AS DepartmentID

FROM Employees

Objective Select all the employees, replace 4 middle numbers with 9999 in DepartmentID:

---4-----4-----4

0007 0128 0918

4 char to the left

4 char to the right

4 char in the middle


Table: Employees

First Column in Table: EmployeeID

Column: DepartmentID

000701280918

000701782662

000702220442

000702118362

000702108487

000702109473

000702033600

000702275707


How would I write the statement

Thank You for your assistance

like image 945
user975828 Avatar asked Jul 21 '26 05:07

user975828


1 Answers

Check out the stuff function:

update  YourTable
set     DepartmentId = stuff(DepartmentId, 5, 4, '9999')
like image 158
Andomar Avatar answered Jul 22 '26 21:07

Andomar