0

I want to convert string to xml column .. I used below query for that :

 Select CONVERT(xml,'<x>' + Replace(A.name,':','</x><x>')+'</x>' ) as xDim from Erecharge;

but it shows error of incorrect sql syntax.. I want to know whats wrong in above query

I also tried this:

 Select Cast('<x>' + Replace(A.name,':','</x><x>')+'</x>' as XML) as xDim from Erecharge;
mansi
  • 837
  • 5
  • 12
  • Please check the error message again. It should mention the exact spot where the typo is. – Álvaro González Oct 12 '18 at 09:20
  • I got this error: Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'XML) as xDim from Erecharge' at line 1 – mansi Oct 12 '18 at 09:32

1 Answers1

0

check the manual that corresponds to your MySQL server version for the right syntax to use near 'XML) as xDim from Erecharge'

This means that XML is incorrect in a expression like this:

CAST('foo' AS XML)

As per the docs, the values allowed for CAST type do not include XML.

Additionally, using the + operator on strings is just a convoluted way to render zero:

mysql> SELECT 'a' + 'b';
+-----------+
| 'a' + 'b' |
+-----------+
|         0 |
+-----------+
1 row in set, 2 warnings (0.00 sec)

It's not entirely clear what you're trying to do. MySQL has XML Functions but it doesn't have XML data types. If you just want to produce a string that happens to contain XML code then you need to CONCAT():

mysql> SELECT CONCAT('<date>', CURRENT_TIMESTAMP, '</date>') AS foo;
+----------------------------------+
| foo                              |
+----------------------------------+
| <date>2018-10-12 11:44:29</date> |
+----------------------------------+
1 row in set (0.00 sec)

... but of course you still need to ensure that angle brackets and similar stuff don't break the XML. CDATA may help. (No idea about XML functions, I'm not familiar with them.)

Álvaro González
  • 142,137
  • 41
  • 261
  • 360