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 '&'.
'&'
in the column (may be using like condition)How do i do that? Pl. help
SQL Server REPLACE() Function The REPLACE() function replaces all occurrences of a substring within a string, with a new substring. Note: The search is case-insensitive.
You can use REPLACE in an UPDATE statement.
If you'd like to replace a substring with another string, simply use the REPLACE function. This function takes three arguments: The string to change (which in our case was a column). The substring to replace.
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 '%&%'
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 &
with &
and then <
with <
, but the result will be <
if you first try to replace <
with <
and then &
with &
.
If I have to do that kind of replacement, I usually replace &
last for this reason. Sure, an edge case, and not something which happens often, but you never know...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With