SQL Help - How do I query a value out of a field that has XML?

I have a text field in a table where the data is stored in XML.  Is there an easy way through SQL statements to query this data?  

I'm using SQL Server 2008.

Thanks!
nightshadzAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nightshadzAuthor Commented:
My XML looks like this -

<person firstName="Jon lastName="Johnson"></person>
0
Anthony PerkinsCommented:
Unfortunately that is not well-formed Xml.  In order for it to be well formed it needs to be:
<person firstName="Jon" lastName="Johnson"></person>
 
Also we need some clarity to the question:
"I have a text field in a table where the data is stored in XML."
So is it text or Xml data type?

If it is Xml than it is as simple as (fix the obvious typos in xSELECT, xDECLARE and xINSERT):
xSELECT      T.C.value('@firstName', 'varchar(20)'),
      T.C.value('@lastName', 'varchar(20)')
FROM      YourTableName
      CROSS APPLY YourXmlColumn.nodes('person') T(C)

This is how I tested it:
xDECLARE @T TABLE (MyXml Xml)

xINSERT @T (MyXml) VALUES ('<person firstName="Jon" lastName="Johnson"></person>')

xSELECT	T.C.value('@firstName', 'varchar(20)'),
	T.C.value('@lastName', 'varchar(20)')
FROM	@T
	CROSS APPLY MyXml.nodes('person') T(C)

Output:
(No column name)	(No column name)
Jon	Johnson

Open in new window

0
nightshadzAuthor Commented:
Must have missed that extra quote - I meant to type <person firstName="Jon" lastName="Johnson"></person>.

The field is defined as AUDITLOG(text, null)
0
Anthony PerkinsCommented:
Then you will have to use a derived table as follows:

SELECT  T.C.value('@firstName', 'varchar(20)'),
             T.C.value('@lastName', 'varchar(20)')
FROM    (SELECT CAST(MyXml AS XML) MyXml FROM YourTable) t1
            CROSS APPLY MyXml.nodes('person') T (C)


This is how I tested it:
DECLARE @T TABLE (MyXml TEXT)

INSERT  @T(MyXml) VALUES ('<person firstName="Jon" lastName="Johnson"></person>')

SELECT  T.C.value('@firstName', 'varchar(20)'),
        T.C.value('@lastName', 'varchar(20)')
FROM    (SELECT CAST(MyXml AS XML) MyXml FROM @T) t1
        CROSS APPLY MyXml.nodes('person') T (C)

Output:
(No column name)	(No column name)
Jon	Johnson

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.