15

Hi I have a column of nvarchar(1000) type. I need to get rid of encode characters from that column and replace them with their special characters. For Example:

column value is : 'This text values contains this '&' this'.

I have to replace '&' with '&'.

  1. First have to find the record which has '&' in the column (may be using like condition)
  2. And then replace only this word with its special character

How do i do that? Pl. help

4 Answers 4

32

This will replace in the entire column

REPLACE(MyColumn, '&', '&')

You'll have to nest other replacements...

REPLACE(REPLACE(MyColumn, '&', '&'), '>', '>')

All together

UPDATE myTable
SET MyColumn = REPLACE(MyColumn, '&', '&')
WHERE MyColumn LIKE '%&%'
Sign up to request clarification or add additional context in comments.

3 Comments

@Jango: sorry, was rushing and only saw the string replace issue
@gb: Thanks but i think it won't work w/o mentioning column name after SET or will it?
@Jango: sorry again. I'm typing on a phone. Not very well either
8
UPDATE mytable
    SET mycol = REPLACE(mycol, N'&', N'&')
    WHERE mycol LIKE '%&%'

EDIT If you decide to replace multiple html entities in one go, the order of the replacements may change results.

For example:

<

becomes &< if you replace first &amp; with & and then &lt; with <, but the result will be &lt; if you first try to replace &lt; with < and then &amp; with &.

If I have to do that kind of replacement, I usually replace &amp; last for this reason. Sure, an edge case, and not something which happens often, but you never know...

1 Comment

Actually, i am doing multiple replacements. You have a point. Thanks.
5

Generic syntax:

UPDATE Table_Name 
SET Column_Name = REPLACE(Column_Name, 'Text_To_Be_Replaced', 'New_Text')
WHERE Column_Name LIKE '%Text_To_Be_Replaced%'

Comments

1
Update TABLE
Set    field = replace(field, '&amp;', '&');

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.