1

I need to insert some xml into a SQL table column that looks like this:

<date format="ddd MMM dd HH:mm:ss \"UTC\" yyyy" />

SQL complains it is expecting whitespace after the double quote before the U.

INSERT INTO foo
(date)
VALUES ('<date format="ddd MMM dd HH:mm:ss \"UTC\" yyyy" />')

I've tried doubling the double quotes and the backslashes, but I get the same error.

Martin Smith
  • 438,706
  • 87
  • 741
  • 845
Dan Bailiff
  • 1,513
  • 5
  • 23
  • 38

2 Answers2

2

Your XML is invalid. " is not allowed in attribute values when you enclose the value with ".

Escape the " with &quot; like this

<date format="ddd MMM dd HH:mm:ss \&quot;UTC\&quot; yyyy" />

Or use ' to enclose the attribute value

<date format='ddd MMM dd HH:mm:ss \"UTC\" yyyy' />

The result in the XML column in SQL Server is the same no matter how you do it.

<date format="ddd MMM dd HH:mm:ss \&quot;UTC\&quot; yyyy"/>
Mikael Eriksson
  • 136,425
  • 22
  • 210
  • 281
0

Use SQL parameters to avoid any issues with the values you have to insert

Ron Harlev
  • 16,227
  • 24
  • 89
  • 132