Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using XQuery 1

Status
Not open for further replies.

sds814

Programmer
Feb 18, 2008
164
US
Code:
DECLARE @myXML XML
SET @myXML = '<SampleXML>
    <Product EffectiveDate="2007-01-01" ProductCode="030" />
    <Product EffectiveDate="2008-01-03" ProductCode="030" />
    <Product EffectiveDate="2007-01-10" ProductCode="030" />
    <Product EffectiveDate="2007-01-20" ProductCode="090" />
    <Product EffectiveDate="2007-01-01" ProductCode="090" />
    <Product EffectiveDate="2005-10-01" ProductCode="090" />
    <Product EffectiveDate="2011-01-21" ProductCode="0BA" />
    <Product EffectiveDate="2007-01-01" ProductCode="0BA" />
</SampleXML>'

select @myXML.value('(/SampleXML/Product/@EffectiveDate)[1]', 'datetime')

Is there any way that I can get all the values for Effective Date? With the query I have I am passing [1]

I'd like to get all the Product Code values also.
 
There's likely to be multiple ways to do this. This is just one of them.

Code:
DECLARE @myXML XML
SET @myXML = '<SampleXML>
    <Product EffectiveDate="2007-01-01" ProductCode="030" />
    <Product EffectiveDate="2008-01-03" ProductCode="030" />
    <Product EffectiveDate="2007-01-10" ProductCode="030" />
    <Product EffectiveDate="2007-01-20" ProductCode="090" />
    <Product EffectiveDate="2007-01-01" ProductCode="090" />
    <Product EffectiveDate="2005-10-01" ProductCode="090" />
    <Product EffectiveDate="2011-01-21" ProductCode="0BA" />
    <Product EffectiveDate="2007-01-01" ProductCode="0BA" />
</SampleXML>'

Declare @iDoc Int

exec sp_xml_preparedocument @iDoc OUTPUT, @myXML

Select *
From   OpenXML(@iDoc, '/SampleXML/Product', 3)
With   (EffectiveDate DateTime,
		ProductCode VarChar(3))

Exec sp_xml_removedocument @iDoc


-George
Microsoft SQL Server MVP
My Blogs
SQLCop
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Here's another way.

Code:
DECLARE @myXML XML
SET @myXML = '<SampleXML>
    <Product EffectiveDate="2007-01-01" ProductCode="030" />
    <Product EffectiveDate="2008-01-03" ProductCode="030" />
    <Product EffectiveDate="2007-01-10" ProductCode="030" />
    <Product EffectiveDate="2007-01-20" ProductCode="090" />
    <Product EffectiveDate="2007-01-01" ProductCode="090" />
    <Product EffectiveDate="2005-10-01" ProductCode="090" />
    <Product EffectiveDate="2011-01-21" ProductCode="0BA" />
    <Product EffectiveDate="2007-01-01" ProductCode="0BA" />
</SampleXML>'

Select Product.value('@EffectiveDate[1]','DateTime') EffectiveDate,
       Product.value('@ProductCode[1]','varchar(3)') As ProductCode
From   @myXML.nodes('SampleXML/Product') Product([Product])

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Thanks George! The second way seems like it's using SQL Server 2005 syntax.
 
Yes. The first method will work on SQL2005 (if you change the XML data type to varchar). The second way will work with SQL2005 and newer.

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top