I'm going batty trying to figure this one out, in fact I thought I had found
a solution last week but when I revisited this to actually implement it
yesterday, my presumed solution was not at all, a solution that is, i.e. it
didn't work!
Here's the scenario (sorry narrative first then DDL and all the good stuff):
I have data that has been imported from another source and has to be
massaged before it can be moved into our production tables. There is a
Description column that contains HTML codes that we need to be translated
into their actual character representations, e.g. " should become ".
The problem I have encountered is that there could be several different HTML
codes in the Description and I can't seem to find an easy way to replace all
of them at one go.
Ok, DDL and stuff:
CREATE TABLE Items
(ItemID INT PRIMARY KEY,
Description VARCHAR(100))
CREATE TABLE HTMLCodes
(HTMLCode CHAR(6) PRIMARY KEY,
HTMLChar CHAR(1))
INSERT INTO Items
VALUES (1, 'Some HTML Codes " ™ ½')
INSERT INTO Items
VALUES (2, 'Some more HTML Codes " ¼ ¾')
INSERT INTO HTMLCodes
VALUES ('"', '"')
INSERT INTO HTMLCodes
VALUES ('™', 'T')
INSERT INTO dbo.HTMLMapping
VALUES ('¼', '?')
INSERT INTO dbo.HTMLMapping
VALUES ('½', '?')
INSERT INTO dbo.HTMLMapping
VALUES ('¾', '?')
Obviously what I would like to see is this:
ItemID Description
------- --------------------------------------------
1 Some HTML Codes " T ?
2 Some more HTML Codes " ? ?
I tried various UPDATEs using REPLACE(Description, HTMLCode, HTMLChar),
here's one example I tried:
UPDATE Items SET
Description = REPLACE(Description, HTMLCode, HTMLChar)
FROM Items AS I JOIN HTMLCodes AS H ON I.Description LIKE '%' + H.HTMLCode
+ '%'
But of course this doesn't work, well not completely, it updates one of the
HTML Codes found but not all of them.
Any idea as to how I can do this?
TIA
Michael MacGregor
Database Architect
SalesDriver