question

czajna avatar image
czajna asked

Select and replace all entries LIKE

Hi All, perhaps it is very simple question, but I could not find a solution for that. I would like to find all entries which consist a word "A" and rename them to "New_Word" for a specific column. Am I able to achieve such result with REPLACE function? SELECT * FROM Table WHERE REPLACE(Column, '%A%', 'New_Word')
replacelike
3 comments
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Do you want to write the changes to the table? Or are you attempting to manipulate a result set for say the benefit of a client with out making permanent changes to the table?
0 Likes 0 ·
I don't want to write the changes to the table, only select the data with renamed entries.
0 Likes 0 ·
This site functions according to votes. For all helpful answers below, show this by clicking on the thumbs up next to those answers. If any one answer lead to a solution, indicate this by clicking on the check mark next to that answer.
0 Likes 0 ·
mjharper avatar image
mjharper answered
You want to move the REPLACE into the SELECT to see the result of that REPLACE. If you want to only return the rows where the column contains your search text then you could use the CHARINDEX function like this: SELECT *, REPLACE(Column, '%A%', 'New_Word') FROM Table WHERE CHARINDEX('A', Column) > 0
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

James_Chandler avatar image
James_Chandler answered
In that case I would use a CASE statement in the Select: SELECT CASE WHEN [Column] LIKE '%A%' THEN 'New_Word' END AS [NewWord] , * FROM [Table]
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.