Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Filtering results from SELECT on XML column


Filtering results from SELECT on XML column

Author
Message
Gary Varga
Gary Varga
SSCrazy Eights
SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)

Group: General Forum Members
Points: 8380 Visits: 6162
Hi I have the following table:

CREATE TABLE XmlTable ([ImportData] [xml] NOT NULL) ON [Primary]



I have the following XML format:

<Parent>
<ID>1234</ID>
<Child>
<State>1</State>
<Value>X</Value>
</Child>
<Child>
<State>2</State>
<Value>Y</Value>
</Child>
</Parent>



and I want to select all the child values like this:

SELECT child.value('(parent::node()/ID/text)[1]', 'NVARCHAR(50)') AS [ID]
, child.value('(State/text)[1]', 'NVARCHAR(50)') AS [State]
, child.value('(Value/text)[1]', 'NVARCHAR(50)') AS [Value]
FROM [XmlTable] AS T CROSS APPLY [ImportData].nodes('/Parent/Child') AS ImportData(child)



I need to restrict (like a WHERE clause) on the value of the State element of the Child element e.g. all States where it isn't 1:

ID          State       Value
1234 2 Y



I have tried various methods for half a day now. Can anyone help? Please?

PS Please excuse any typos as I am working in a Hyper-V VM with wireless network access so it has all been retyped by hand as I cannot access network from VM :-(

Gaz

-- Stop your grinnin' and drop your linen...they're everywhere!!!
Jack Corbett
  Jack Corbett
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11034 Visits: 14858
Pretty sure you want to use the .exist() method. Check out his blog post about it: http://www.jasonstrate.com/2010/11/xquery-for-the-non-expert-exist/



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Gary Varga
Gary Varga
SSCrazy Eights
SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)

Group: General Forum Members
Points: 8380 Visits: 6162
Jack Corbett (4/5/2012)
Pretty sure you want to use the .exist() method. Check out his blog post about it: http://www.jasonstrate.com/2010/11/xquery-for-the-non-expert-exist/


Thanks Jack (sorry for being tardy with this overdue gratitude!!!)

Gaz

-- Stop your grinnin' and drop your linen...they're everywhere!!!
Mark Cowne
Mark Cowne
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2095 Visits: 22803
Also this

SELECT child.value('(parent::node()/ID/text())[1]', 'NVARCHAR(50)') AS [ID]
, child.value('(State/text())[1]', 'NVARCHAR(50)') AS [State]
, child.value('(Value/text())[1]', 'NVARCHAR(50)') AS [Value]
FROM [XmlTable] AS T
CROSS APPLY [ImportData].nodes('/Parent/Child[State/text() != "1"]') AS ImportData(child)



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search