0

I have some data in this format:

52-681-Functional-EHT
53-682-Functional-EGT
52-683-Functional-EZT
51-684-Functional-EYT
59-685-Functional-E5T

And I want to remove the interior '-Functional' permanently with a sql statement so that my records would now look like...

52-681-EHT
53-682-EGT
52-683-EZT
51-684-EYT
59-685-E5T

All my research leads me to Replace but that seems to just be on a select and can't quite figure out how to form it into an appropriate update statement. Again, I want to remove the string FOREVER.

enter image description here

Creation script:

create table part (
id int identity(1,1) PRIMARY KEY,
Partname nvarchar(50))

insert into Part (Partname)
values ('52-681-Functional-EHT'),
('53-682-Functional-EGT'),
('52-683-Functional-EZT'),
('51-684-Functional-EYT'),
('59-685-Functional-E5T')
1

2 Answers 2

2

Is there a reason this simple update doesn't work for you?

update part set partname = replace(partname,'functional-','')

select * from part
1
  • And... now I'm embarrassed. Commented Apr 6, 2017 at 15:27
1

Using a REPLACE in an UPDATE is fairly straightforward. In your case, you would do something like this:

UPDATE p
SET Partname = REPLACE(Partname,'-Functional','')
FROM #part p;

However, there are always different ways to solve the same problem. You could also handle the update by using SUBSTRING to build the desired value based on character position:

UPDATE p
SET Partname = SUBSTRING(Partname,1,6) + SUBSTRING(Partname,18,4)
FROM #part p;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.