<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: Remove illegal char from xml column in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaa-p/13841437#M4872280</link>
    <description>&lt;P&gt;You guys are right. Having looked more carefully at the table, I've notice the presence of a trigger that indeed does some parsing on update (disabling the trigger allowed the updates of fixing the xml just fine). Can't believe that didn't cross my mind! So there you go, case closed. Thank you!&lt;/P&gt;</description>
    <pubDate>Fri, 06 Jun 2014 00:00:00 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2014-06-06T00:00:00Z</dc:date>
    <item>
      <title>Remove illegal char from xml column</title>
      <link>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaq-p/13841423</link>
      <description>&lt;P&gt;So I've inherited this database with some invalid xml data in one table. &lt;/P&gt;
&lt;P&gt;I know for a fact that the character is a control char and I can find it in the column (which is of type XML):&lt;/P&gt;
&lt;P&gt;&lt;CODE&gt;SELECT CHARINDEX(char(26), BLA_COLUMN) FROM BLA_TABLE where ID_RECORD = 1234&lt;/CODE&gt;
Returns a valid index.
Now, when I try to replace that char with something else: &lt;/P&gt;
&lt;P&gt;&lt;CODE&gt;UPDATE BLA_TABLE SET BLA_COLUMN = REPLACE(BLA_COLUMN, char(26), char(32)) WHERE ID_RECORD = 1234&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;The statement fails with message: "XML parser error: character: 603, line:1, column:603 
Illegal control character SQLCODE=-888, ODBC 3 State="HY000"&lt;/P&gt;
&lt;P&gt;Any ideas how to get rid of that character? 
(Running SQLA 12)&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;EDIT&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Looks like Volker's suggestion to first update the BLA_COLUMN to a Null value then to update to a valid XML, replacing char(26) with char (32), provides a workaround. Thanks Volker!&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jun 2014 06:05:53 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaq-p/13841423</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-06-05T06:05:53Z</dc:date>
    </item>
    <item>
      <title>Re: Remove illegal char from xml column</title>
      <link>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaa-p/13841424#M4872267</link>
      <description>&lt;P&gt;I think you need to convert the xml data to something that SQLA doesn't try to interpret.&lt;/P&gt;
&lt;P&gt;eg (untested)&lt;/P&gt;
&lt;PRE&gt;create variable mytext long varchar;
set mytext = (SELECT (BLA_COLUMN) FROM BLA_TABLE where ID_RECORD = 1234);
set mytext= REPLACE(mytext, char(26), char(32));
update BLA_TABLE set BLA_COLUMN = mytext where ID_RECORD = 1234;
commit;
&lt;/PRE&gt;

&lt;P&gt;If you have to do this a lot you could write a user defined function. Also if you have characters which have problems because of collation tables eg
&lt;/P&gt;&lt;PRE&gt;update person set notes=replace(notes,'ú','£')&lt;/PRE&gt;
and every u in the field will also be changed as the collation sequence
helpfully reckons that 'ú' is the same as 'u', you can use this approach:&lt;P&gt;&lt;/P&gt;
&lt;PRE&gt;create function BinaryReplace(in x long varchar,in targetascii smallint,
in replacementascii smallint) returns long varchar deterministic begin
  declare rv long varchar;
  declare l integer;
  declare i integer;
  declare c char(1);
  set l=length(x);
  set i=0;
  set rv='';
  while i &amp;lt; l loop
    set i=i+1;
    set c=substr(x,i,1);
    if ascii(c) = targetascii then
      set c=char(replacementascii)
    end if;
    set rv=rv+c
  end loop;
  return rv
end;
&lt;/PRE&gt;

&lt;P&gt;not fast, but quicker than typing!&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jun 2014 06:47:08 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaa-p/13841424#M4872267</guid>
      <dc:creator>justin_willey</dc:creator>
      <dc:date>2014-06-05T06:47:08Z</dc:date>
    </item>
    <item>
      <title>Re: Remove illegal char from xml column</title>
      <link>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaa-p/13841426#M4872269</link>
      <description>&lt;P&gt;Weird! I can't even run a statement like:
UPDATE BLA_TABLE set BLA_COLUMN = '&amp;lt;bla/&amp;gt;' where ID_RECORD=1234;
without the XML parser complaining as above&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jun 2014 07:43:53 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaa-p/13841426#M4872269</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-06-05T07:43:53Z</dc:date>
    </item>
    <item>
      <title>Re: Remove illegal char from xml column</title>
      <link>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaa-p/13841427#M4872270</link>
      <description>&lt;P&gt;Also untested...&lt;/P&gt;
&lt;PRE&gt;UPDATE BLA_TABLE 
   SET BLA_COLUMN = REPLACE ( CAST ( BLA_COLUMN, LONG VARCHAR ), 
                              char(26), char(32)) 
 WHERE ID_RECORD = 1234
&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Jun 2014 07:46:02 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaa-p/13841427#M4872270</guid>
      <dc:creator>Breck_Carter</dc:creator>
      <dc:date>2014-06-05T07:46:02Z</dc:date>
    </item>
    <item>
      <title>Re: Remove illegal char from xml column</title>
      <link>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaa-p/13841425#M4872268</link>
      <description>&lt;P&gt;Is the same true if you try to set the column to null (which would look like the previous contents is parsed, too)...?&lt;/P&gt;
&lt;HR /&gt;
&lt;P&gt;EDIT: As this seems to have done the trick, it shows that&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;the parser does not validate the old contents (preventing some kind of "correction deadlock")&lt;/LI&gt;
&lt;LI&gt;and NULL is not that bad now and then(Breck, do you hear me?:)&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Thu, 05 Jun 2014 07:48:58 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaa-p/13841425#M4872268</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2014-06-05T07:48:58Z</dc:date>
    </item>
    <item>
      <title>Re: Remove illegal char from xml column</title>
      <link>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaa-p/13841428#M4872271</link>
      <description>&lt;P&gt;Is Breck's method allowed to work without the parser complaining?&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jun 2014 08:25:03 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaa-p/13841428#M4872271</guid>
      <dc:creator>justin_willey</dc:creator>
      <dc:date>2014-06-05T08:25:03Z</dc:date>
    </item>
    <item>
      <title>Re: Remove illegal char from xml column</title>
      <link>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaa-p/13841429#M4872272</link>
      <description>&lt;P&gt;If that is happening it's hard to see how you would ever be able to fix it - it's odd that it could get in there in the first place unless the behaviour has been changed at some time and it is now stricter. If this is the case, it's beginning to look more like a bug than a feature &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jun 2014 08:33:34 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaa-p/13841429#M4872272</guid>
      <dc:creator>justin_willey</dc:creator>
      <dc:date>2014-06-05T08:33:34Z</dc:date>
    </item>
    <item>
      <title>Re: Remove illegal char from xml column</title>
      <link>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaa-p/13841430#M4872273</link>
      <description>&lt;P&gt;Same error as above unfortunately&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jun 2014 08:42:10 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaa-p/13841430#M4872273</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-06-05T08:42:10Z</dc:date>
    </item>
    <item>
      <title>Re: Remove illegal char from xml column</title>
      <link>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaa-p/13841431#M4872274</link>
      <description>&lt;P&gt;Setting the column to Null first, then updating it with a valid XML (ie char 26 replaced with char 32) seems to do the trick.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jun 2014 08:45:58 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaa-p/13841431#M4872274</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-06-05T08:45:58Z</dc:date>
    </item>
    <item>
      <title>Re: Remove illegal char from xml column</title>
      <link>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaa-p/13841432#M4872275</link>
      <description>&lt;P&gt;Do you have a support contract with SAP - it's beginning to look like you may need to open a case.&lt;/P&gt;
&lt;P&gt;But in the meantime:
The docs say: &lt;I&gt;&lt;B&gt;You can cast between the XML data type and any other data type that can be cast to or from a string. Note that there is no checking that the string is well-formed when it is cast to XML.&lt;/B&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;So it's looking a bit like you can write in anything, but can only read out well formed XML.&lt;/P&gt;
&lt;P&gt;To check this can you try just selecting the XML into a string without trying to write anything back - I'm just wondering if there is more than one illegal character which is why Breck's method fails. &lt;/P&gt;
&lt;P&gt;ie something like
&lt;/P&gt;&lt;PRE&gt;create variable mytext long varchar;
set mytext = (SELECT (BLA_COLUMN) FROM BLA_TABLE where ID_RECORD = 1234);
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;
&lt;P&gt;OR&lt;/P&gt;
&lt;PRE&gt;create variable mytext long varchar;
set mytext = (SELECT cast(BLA_COLUMN as long varchar) FROM BLA_TABLE where ID_RECORD = 1234);
&lt;/PRE&gt;

&lt;P&gt;and see if you can then read what is in mytext.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jun 2014 08:48:51 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaa-p/13841432#M4872275</guid>
      <dc:creator>justin_willey</dc:creator>
      <dc:date>2014-06-05T08:48:51Z</dc:date>
    </item>
    <item>
      <title>Re: Remove illegal char from xml column</title>
      <link>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaa-p/13841433#M4872276</link>
      <description>&lt;P&gt;Reading works fine in both cases. The updating with a valid XML proved to be tricky. (Please see my Edit)&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jun 2014 09:17:52 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaa-p/13841433#M4872276</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-06-05T09:17:52Z</dc:date>
    </item>
    <item>
      <title>Re: Remove illegal char from xml column</title>
      <link>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaa-p/13841434#M4872277</link>
      <description>&lt;P&gt;I had a go reproducing the problem (on 10.0.1 and 16.0) but couldn't - I could select and update the bad XML without an issue - so I was wondering what was causing the problem. Anyway, you have a work around so that's great. &lt;/P&gt;
&lt;PRE class="codehilite"&gt;&lt;CODE class="language-xml"&gt;create table TestXML(PK int default autoincrement, XMLStuff xml, primary key(PK));

insert into TestXML(XMLStuff) values ('&amp;lt;Bad XML&amp;gt;');
insert into  TestXML(XMLStuff) values ('&amp;lt;GoodXML/&amp;gt;');
insert into  TestXML(XMLStuff) values (string('&amp;lt;ReallyBadXML',char(26),'&amp;gt;'));
insert into  TestXML(XMLStuff) values (string('&amp;lt;EvenWorseXML',char(26)));

select * from TestXML where PK = 1;
-- works OK

update TestXML set XMLStuff = '&amp;lt;BetterXML/&amp;gt;' where  PK = 1;
-- works OK

select * from TestXML where PK = 3;
-- works OK

update TestXML set XMLStuff = '&amp;lt;BetterXML/&amp;gt;' where  PK = 3;
-- works OK

select * from TestXML where PK = 4;
-- works OK

update TestXML set XMLStuff = '&amp;lt;BetterXML/&amp;gt;' where  PK = 4;
-- works OK

drop table TestXML;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Jun 2014 10:00:10 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaa-p/13841434#M4872277</guid>
      <dc:creator>justin_willey</dc:creator>
      <dc:date>2014-06-05T10:00:10Z</dc:date>
    </item>
    <item>
      <title>Re: Remove illegal char from xml column</title>
      <link>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaa-p/13841435#M4872278</link>
      <description>&lt;P&gt;12.0.1.4085 shows the same success.&lt;/P&gt;
&lt;P&gt;Also trying to turn a valid XML into an invalid one doesn't raise an error, such as:&lt;/P&gt;
&lt;PRE class="codehilite"&gt;&lt;CODE class="language-sql"&gt;update TestXML set XMLStuff =
  replace(XMLStuff, char(47), char(26)) where  PK = 2;
-- works OK&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Jun 2014 10:39:28 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaa-p/13841435#M4872278</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2014-06-05T10:39:28Z</dc:date>
    </item>
    <item>
      <title>Re: Remove illegal char from xml column</title>
      <link>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaa-p/13841436#M4872279</link>
      <description>&lt;P&gt;SQL Anywhere only validates XML when parsing it (for example, using OPENXML). It is not validated when inserting or updating a value, or casting to the XML type. I wonder if the original database has a trigger, computed column, or check constraint that ends up parsing the XML.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jun 2014 14:31:15 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaa-p/13841436#M4872279</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-06-05T14:31:15Z</dc:date>
    </item>
    <item>
      <title>Re: Remove illegal char from xml column</title>
      <link>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaa-p/13841437#M4872280</link>
      <description>&lt;P&gt;You guys are right. Having looked more carefully at the table, I've notice the presence of a trigger that indeed does some parsing on update (disabling the trigger allowed the updates of fixing the xml just fine). Can't believe that didn't cross my mind! So there you go, case closed. Thank you!&lt;/P&gt;</description>
      <pubDate>Fri, 06 Jun 2014 00:00:00 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/remove-illegal-char-from-xml-column/qaa-p/13841437#M4872280</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-06-06T00:00:00Z</dc:date>
    </item>
  </channel>
</rss>

