1

I have a XML column in a SQL Server database table. It stores XML responses from a web service.

If that web service times out, it stores the actual text

Error: Timed out waiting for a reply

It's actually storing that text in the XML column - and I can't seem to query it because I cannot compare a string to an XML value. Any ideas?

3
  • Just wrap it into a <ErrorMsg> ..... </ErrorMsg> XML wrapper? Commented May 29, 2020 at 19:54
  • Woah, scary... TIL that SQL Server doesn't actually confirm that data inserted into XML columns really is well-formed XML: create table dbo.Test ( Foo xml ); insert dbo.Test (Foo) values (N'Error: Timed out waiting for a reply'); select * from dbo.Test; Commented May 29, 2020 at 23:45
  • @AlwaysLearning - the XML datatype supports XML fragments. You don't have to give it a complete well formed document. In the case of a string like 'Error: Timed out waiting for a reply' - this is just treated as a single text node. This is why the XML PATH string concatenation method works and doesn't complain that the output isn't valid XML Commented May 30, 2020 at 9:58

1 Answer 1

2

A non empty string without any tags will be treated as a standalone text node in SQL Server.

So you can use 'text()[1]' to get the first top level text node in the XML content and inspect that.

you can use exist if you want to exclude from consideration any content that is a mix of top level text nodes and elements.

SELECT *,
        your_col.value('text()[1]', 'varchar(8000)')
FROM t
WHERE your_col.exist('*') = 0 AND your_col.exist('text()') = 1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.