SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Querying specific values in column that's using XML data


Querying specific values in column that's using XML data

Author
Message
Chuck
Chuck
Valued Member
Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)

Group: General Forum Members
Points: 63 Visits: 24
Boy, I've been researching this subject, but I am just not getting it. Sorry if I'm asking a question that has all ready been asked a million times, but its hard to understand when you're a noob like me, and the values in my tables aren't like the others I've seen. So here it goes...

In my SQL Server database, I have a table that has all my listings in it, called ItemsEbay.


(The main identifier for each individual item has a column named ItemID.)

Within the ItemsEbay table is a column named ItemSpecifics that contains XML data. Within the ItemSpecifics XML Data, is a node with a <Name> of UPC, and text and random <value> for that node:


What I would like, is a query that would allow me to search all the items in the ItemsEbay table that have a specific UPC "value" of my choosing, such as TEST10, or 10U100 for instance.

When the query finds the matching values I'm looking for, I would like to be able to replace them all at once with a new value "Does Not Apply."
Any help would be greatly appreciated.


Steve Jones
Steve Jones
SSC Guru
SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)

Group: Administrators
Points: 603907 Visits: 21115
You need XQuery to do this. You can search a series of XML documents in rows and then update then. It's not easy or pretty.

This article might help: http://www.sqlservercentral.com/articles/XML/67548/

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Eirikur Eiriksson
Eirikur Eiriksson
SSC Guru
SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)

Group: General Forum Members
Points: 159635 Visits: 23351
c_wager - Thursday, November 16, 2017 7:16 AM
Boy, I've been researching this subject, but I am just not getting it. Sorry if I'm asking a question that has all ready been asked a million times, but its hard to understand when you're a noob like me, and the values in my tables aren't like the others I've seen. So here it goes...

In my SQL Server database, I have a table that has all my listings in it, called ItemsEbay.


(The main identifier for each individual item has a column named ItemID.)

Within the ItemsEbay table is a column named ItemSpecifics that contains XML data. Within the ItemSpecifics XML Data, is a node with a <Name> of UPC, and text and random <value> for that node:


What I would like, is a query that would allow me to search all the items in the ItemsEbay table that have a specific UPC "value" of my choosing, such as TEST10, or 10U100 for instance.

When the query finds the matching values I'm looking for, I would like to be able to replace them all at once with a new value "Does Not Apply."
Any help would be greatly appreciated.


This is trivial stuff but one cannot use pictures of data to provide examples, can you please post a sample XML and the expected results!
Cool

Eirikur Eiriksson
Eirikur Eiriksson
SSC Guru
SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)

Group: General Forum Members
Points: 159635 Visits: 23351
Steve Jones - SSC Editor - Thursday, November 16, 2017 7:52 AM
It's not easy or pretty.

Everything is easy if you know it and beuty is in the eye of the beholderWink
Cool

Steve Jones
Steve Jones
SSC Guru
SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)SSC Guru (603K reputation)

Group: Administrators
Points: 603907 Visits: 21115
It's definitely not pretty, and doesn't perform well. Learning XQuery isn't easy, either.

You may think it's easy now, Eirikur, but I suspect you're discounting your efforts in the past to get here.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Thom A
Thom A
SSC Guru
SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)

Group: General Forum Members
Points: 82523 Visits: 21130
I have to agree with Steve, I'm not a fan of XQuery. I (normally) have to look up the syntax each and every time I have to use it.

To the OP, this might get you on the right path. I'm not quite sure what you want as your WHERE, but the syntax to get you on track is there:
CREATE TABLE #XML (XMLData xml);

INSERT INTO #XML
VALUES (
'<SelectedValues>
<SelectedValue>
<Name>UPC</Name>
<Value>Test10</Value>
</SelectedValue>
<SelectedValue>
<Name>Brand</Name>
<Value>HP</Value>
</SelectedValue>
<SelectedValue>
<Name>UPC</Name>
<Value>Test11</Value>
</SelectedValue>
</SelectedValues>'
)
GO

SELECT SV.S.value('(Value/text())[1]','varchar(10)') AS TextValue
FROM #XML X
CROSS APPLY X.XMLData.nodes('/SelectedValues/SelectedValue') SV (s)
WHERE SV.S.value('(Name/text())[1]','varchar(10)') = 'UPC';

GO
DROP TABLE #XML;



Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does :-P

Please always remember to encapsulate your code in IFCode Markup. For example [code=sql] [/code].
Click here to read Jeffs Guide on how to post SQL questions, and get swift and helpful answers from the community
Chuck
Chuck
Valued Member
Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)

Group: General Forum Members
Points: 63 Visits: 24
Thom A - Thursday, November 16, 2017 8:47 AM
I have to agree with Steve, I'm not a fan of XQuery. I (normally) have to look up the syntax each and every time I have to use it.

To the OP, this might get you on the right path. I'm not quite sure what you want as your WHERE, but the syntax to get you on track is there:
CREATE TABLE #XML (XMLData xml);

INSERT INTO #XML
VALUES (
'<SelectedValues>
<SelectedValue>
<Name>UPC</Name>
<Value>Test10</Value>
</SelectedValue>
<SelectedValue>
<Name>Brand</Name>
<Value>HP</Value>
</SelectedValue>
<SelectedValue>
<Name>UPC</Name>
<Value>Test11</Value>
</SelectedValue>
</SelectedValues>'
)
GO

SELECT SV.S.value('(Value/text())[1]','varchar(10)') AS TextValue
FROM #XML X
CROSS APPLY X.XMLData.nodes('/SelectedValues/SelectedValue') SV (s)
WHERE SV.S.value('(Name/text())[1]','varchar(10)') = 'UPC';

GO
DROP TABLE #XML;

For Where, I'm only wanting to look at the values assigned to the UPC node.
('/SelectedValues/SelectedValue[Name="UPC" and Value[contains(.,"TEST10")] ]') = 1 )) 

Eirikur Eiriksson
Eirikur Eiriksson
SSC Guru
SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)

Group: General Forum Members
Points: 159635 Visits: 23351
Steve Jones - SSC Editor - Thursday, November 16, 2017 8:35 AM
It's definitely not pretty, and doesn't perform well. Learning XQuery isn't easy, either.

You may think it's easy now, Eirikur, but I suspect you're discounting your efforts in the past to get here.

It's better than XPath which I used back in 1999/2000 until 2007/8 when XQuery came out. In fact I think XQuery is as not that bad, the SQL Server implementation is not perfect though.
Cool

Eirikur Eiriksson
Eirikur Eiriksson
SSC Guru
SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)

Group: General Forum Members
Points: 159635 Visits: 23351
c_wager - Thursday, November 16, 2017 1:30 PM
Thom A - Thursday, November 16, 2017 8:47 AM
I have to agree with Steve, I'm not a fan of XQuery. I (normally) have to look up the syntax each and every time I have to use it.

To the OP, this might get you on the right path. I'm not quite sure what you want as your WHERE, but the syntax to get you on track is there:
CREATE TABLE #XML (XMLData xml);

INSERT INTO #XML
VALUES (
'<SelectedValues>
<SelectedValue>
<Name>UPC</Name>
<Value>Test10</Value>
</SelectedValue>
<SelectedValue>
<Name>Brand</Name>
<Value>HP</Value>
</SelectedValue>
<SelectedValue>
<Name>UPC</Name>
<Value>Test11</Value>
</SelectedValue>
</SelectedValues>'
)
GO

SELECT SV.S.value('(Value/text())[1]','varchar(10)') AS TextValue
FROM #XML X
CROSS APPLY X.XMLData.nodes('/SelectedValues/SelectedValue') SV (s)
WHERE SV.S.value('(Name/text())[1]','varchar(10)') = 'UPC';

GO
DROP TABLE #XML;

For Where, I'm only wanting to look at the values assigned to the UPC node.
('/SelectedValues/SelectedValue[Name="UPC" and Value[contains(.,"TEST10")] ]') = 1 )) 

How about posting the sample data I've asked for?
Cool

Eirikur Eiriksson
Eirikur Eiriksson
SSC Guru
SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)

Group: General Forum Members
Points: 159635 Visits: 23351
Thom A - Thursday, November 16, 2017 8:47 AM
I have to agree with Steve, I'm not a fan of XQuery. I (normally) have to look up the syntax each and every time I have to use it.

That's like using the Hungarian Dictionary
Cool

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