Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8270429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:33:52+00:00 2026-06-08T06:33:52+00:00

I have a table with nvarchar(max) column that may contain xml of known structure.

  • 0

I have a table with nvarchar(max) column that may contain xml of known structure. I’d like to parse it to perform some aggregations so need to filter out “bad” entries. Here is the test case:

create table TestTable ([Message] nvarchar(max))
insert into TestTable ([Message]) values 
    ('<root m="1"/>'),
    ('<root m="7"/>'),
    ('<rooo')
go

set quoted_identifier on
go   

create view TestView as
select data.value('(/root/@m)[1]', 'int') as MyValue
from (
    select cast([Message] as xml) as data
    from (
        select [Message] from dbo.TestTable where [Message] like '<root%>'
    ) as T1
) as T2
where data.exist('/root') = 1
go

select * from TestView

This produces:

Msg 9400, Level 16, State 1, Line 1 XML parsing: line 1, character 5,
unexpected end of input

I don’t understand why because if i run the nested query:

    select cast([Message] as xml) as data
    from (
        select [Message] from dbo.TestTable where [Message] like '<root%>'
    ) as T1

it perfectly returns 2 valid rows. Why??

p.s.
Microsoft SQL Server 2008 (SP3) – 10.0.5500.0 (X64) Sep 21 2011 22:45:45 Copyright (c) 1988-2008 Microsoft Corporation Express Edition (64-bit) on Windows NT 6.1 (Build 7601: Service Pack 1)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T06:33:54+00:00Added an answer on June 8, 2026 at 6:33 am

    Your issue is not in the select clause but the where clause:

    where data.exist('/root') = 1
    

    I suspect you are thinking: “Aha! if the xml is not formatted properly then this will return 0 or NULL”. Nope, this function — as with the other xml functions — requires valid xml. Or it gets an error.

    You might be interested in SQL Server: inline conditional convert with XML? and In SQL Server, what is the best way to determine if a given string is a valid XML or not?.

    There does not appear to be a simple way to do what you want. However, you might be able to do a simple check on the original string to see if it is reasonable xml. For instance, the following checks if there are an equal number of “<” and “>”:

    select (case when len(replace(val, '<', '')) = len(replace(val, '>', ''))
                 then 'MAYBE OKAY'
                 else 'NOPE'
            end)
    from (select '<badness' as val) t
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table cell of type nvarchar(max) that typically looks like this: A03
Hey guys, I have a column in an sql server table that is nvarchar(max).
I have XML column in my table which can contain XML like these 2
I have a NVARCHAR(max) column in a table and a stored procedure that would
I have a column of type 'nvarchar(max)' that should now hold XML information instead
I have a column of type nvarchar(max) in a table that contains the substring
I have a table which has a NVARCHAR column, which could contain a GUID
I have a table that looks like this: ID (pk,int) Col1 (nvarchar) Col2 (nvarchar)
I have a table in the database where a Description column is of nvarchar(MAX)
I have a table with nvarchar(max) datatype column. Max length of data in this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.