August 31, 2009 at 4:02 pm
I have a table with and XML column similar to the example below:
CREATE TABLE Test1 (ItemID int, SerialNumbers XML)
INSERT INTO Test1 (ItemID, SerialNumbers) VALUES (1,
'
')
INSERT INTO Test1 (ItemID, SerialNumbers) VALUES (2,
'
')
I am trying to come up with a query that will return the ItemID for each record where a specified serial number exists in the XML.
Is there a way to query the table and return the ItemID if the specified SN exists in the XML column?
Thanks for any help you can provide.
August 31, 2009 at 4:20 pm
August 31, 2009 at 4:24 pm
If so, you can't query the XML because there are no nodes defined. You'll need to convert it to a varchar and use standard character handling functions.
SELECT *
FROM #Test1
WHERE CHARINDEX('456789',CONVERT(varchar(max),SerialNumbers)) > 0
August 31, 2009 at 4:35 pm
Mark: Please edit your post and add [ code ] tags to make your SQL and XML code correctly visible.
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
August 31, 2009 at 4:38 pm
August 31, 2009 at 4:54 pm
My appologies for the missing tags. Below is the code.
I have a table with and XML column similar to the example below:
CREATE TABLE Test1 (ItemID int, SerialNumbers XML);
GO
INSERT INTO Test1 (ItemID, SerialNumbers) VALUES (1,
'
123456
654321
123654
321456
');
INSERT INTO Test1 (ItemID, SerialNumbers) VALUES (2,
'
456789
987654
456987
456987
');
I am trying to come up with a query that will return the ItemID for each record where a specified serial number exists in the XML.
Is there a way to query the table and return the ItemID if the specified SN exists in the XML column?
Thanks for any help you can provide.
August 31, 2009 at 7:01 pm
thanks, Mark.
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
September 1, 2009 at 2:59 am
select itemid,
tbl.col.value('(.)[1]','int')
from Test1 cross apply SerialNumbers.nodes ('/SERIALNUMBERS/SERIALNUMBER') tbl (col)
If what you need is to get the ItemID of a known serial number, you can use the exists method:
select itemid
from Test1
where SerialNumbers.exist('/SERIALNUMBERS/SERIALNUMBER/text()[.="123654"]')=1
Adi
--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
September 1, 2009 at 7:11 am
Thanks! That works great.
I really appreciate the help.
Mark
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply