0

How can I update some of columns:

UPDATE Product
SET Title = REPLACE(Title , 'mn%', 'za%')
WHERE (Title LIKE N'mn%')

Consider data of one column is mnfmnd and must be changed to zafmnd

6
  • is it always the first two characters? Commented Aug 25, 2016 at 19:50
  • @scsimon yes, it's always first characters Commented Aug 25, 2016 at 19:51
  • 2
    Your query is trying to replace all instances of the string literal mn% in Title. I don't think that is what you want. This seems like a great place to use STUFF Commented Aug 25, 2016 at 19:52
  • @SeanLange seems like a great suggestion. Commented Aug 25, 2016 at 19:54
  • @SeanLange it seems to be good, I checking it Commented Aug 25, 2016 at 19:54

4 Answers 4

4

I would be inclined to use stuff() for this purpose:

update product
    set title = stuff(title, 1, 2, 'za')
    where title like 'mn%';
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you , it works :), but sorry I must to choose one answer
2

Try using STUFF as Sean suggested

update Product
set Title = STUFF(Title,1,2,'za')
where title like 'mn%'

1 Comment

Thank you , it works :), but sorry I must to choose one answer
2
create table #t (title varchar(16))
insert into #t (title) values ('abcdef'),('defghi')

UPDATE       #t
SET          title = stuff(Title , 1,2,'za')
WHERE        (Title LIKE N'ab%')

select * from #t

1 Comment

Thank you , it works :), but sorry I must to choose one answer
2

use right and + instead... the % is not a wild card in the replace.

UPDATE       Product
SET          Title = 'za'+right(len(title)-2)
WHERE        (Title LIKE N'mn%')

Incase stuff isn't in your version of SQL server...

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.