Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2005 > SQL Server 2005
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 24th, 2008, 04:08 PM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default XML question for you XML Gurus

Given the following XML, how would I return the BranchID, PeriodType, StartDate and EndDate all in the same query:

I have the following, and I want to add the id attribute from the Branch element:

SELECT
x.value('@pn[1]', 'tinyint') AS PeriodNumber,
x.value('StartDate[1]','datetime') AS StartDate,
x.value('EndDate[1]','datetime') AS EndDate
FROM @dates.nodes('/root/Branch/PeriodType[@pt="0"]/PeriodNumber') d(x)


    DECLARE @dates xml, @startdate datetime

    SET @dates = '<root>
                    <Branch id="1">
                        <PeriodType pt="0">
                            <PeriodNumber pn="1">
                                <StartDate>06/23/2008</StartDate>
                                <EndDate>06/24/2008</EndDate>
                            </PeriodNumber>
                            <PeriodNumber pn="2">
                                <StartDate>06/16/2008</StartDate>
                                <EndDate>06/22/2008</EndDate>
                            </PeriodNumber>
                            <PeriodNumber pn="3">
                                <StartDate>06/09/2008</StartDate>
                                <EndDate>06/15/2008</EndDate>
                            </PeriodNumber>
                            <PeriodNumber pn="4">
                                <StartDate>06/02/2008</StartDate>
                                <EndDate>06/08/2008</EndDate>
                            </PeriodNumber>
                        </PeriodType>
                        <PeriodType pt="1">
                            <PeriodNumber pn="1">
                                <StartDate>06/01/2008</StartDate>
                                <EndDate>06/23/2008</EndDate>
                            </PeriodNumber>
                            <PeriodNumber pn="2">
                                <StartDate>05/01/2008</StartDate>
                                <EndDate>05/31/2008</EndDate>
                            </PeriodNumber>
                            <PeriodNumber pn="3">
                                <StartDate>04/01/2008</StartDate>
                                <EndDate>04/30/2008</EndDate>
                            </PeriodNumber>
                            <PeriodNumber pn="4">
                                <StartDate>03/01/2008</StartDate>
                                <EndDate>03/31/2008</EndDate>
                            </PeriodNumber>
                        </PeriodType>
                        <PeriodType pt="2">
                            <PeriodNumber pn="1">
                                <StartDate>04/01/2008</StartDate>
                                <EndDate>06/23/2008</EndDate>
                            </PeriodNumber>
                            <PeriodNumber pn="2">
                                <StartDate>01/01/2008</StartDate>
                                <EndDate>03/31/2008</EndDate>
                            </PeriodNumber>
                            <PeriodNumber pn="3">
                                <StartDate>10/01/2007</StartDate>
                                <EndDate>12/31/2007</EndDate>
                            </PeriodNumber>
                            <PeriodNumber pn="4">
                                <StartDate>07/01/2007</StartDate>
                                <EndDate>09/30/2007</EndDate>
                            </PeriodNumber>
                        </PeriodType>
                    </Branch>
                    <Branch id="2">
                        <PeriodType pt="0">
                            <PeriodNumber pn="1">
                                <StartDate>06/23/2006</StartDate>
                                <EndDate>06/24/2006</EndDate>
                            </PeriodNumber>
                            <PeriodNumber pn="2">
                                <StartDate>06/16/2006</StartDate>
                                <EndDate>06/22/2006</EndDate>
                            </PeriodNumber>
                            <PeriodNumber pn="3">
                                <StartDate>06/09/2006</StartDate>
                                <EndDate>06/15/2006</EndDate>
                            </PeriodNumber>
                            <PeriodNumber pn="4">
                                <StartDate>06/02/2006</StartDate>
                                <EndDate>06/08/2006</EndDate>
                            </PeriodNumber>
                        </PeriodType>
                        <PeriodType pt="1">
                            <PeriodNumber pn="1">
                                <StartDate>06/01/2006</StartDate>
                                <EndDate>06/23/2006</EndDate>
                            </PeriodNumber>
                            <PeriodNumber pn="2">
                                <StartDate>05/01/2006</StartDate>
                                <EndDate>05/31/2006</EndDate>
                            </PeriodNumber>
                            <PeriodNumber pn="3">
                                <StartDate>04/01/2006</StartDate>
                                <EndDate>04/30/2006</EndDate>
                            </PeriodNumber>
                            <PeriodNumber pn="4">
                                <StartDate>03/01/2006</StartDate>
                                <EndDate>03/31/2006</EndDate>
                            </PeriodNumber>
                        </PeriodType>
                        <PeriodType pt="2">
                            <PeriodNumber pn="1">
                                <StartDate>04/01/2006</StartDate>
                                <EndDate>06/23/2006</EndDate>
                            </PeriodNumber>
                            <PeriodNumber pn="2">
                                <StartDate>01/01/2006</StartDate>
                                <EndDate>03/31/2006</EndDate>
                            </PeriodNumber>
                            <PeriodNumber pn="3">
                                <StartDate>10/01/2005</StartDate>
                                <EndDate>12/31/2005</EndDate>
                            </PeriodNumber>
                            <PeriodNumber pn="4">
                                <StartDate>07/01/2005</StartDate>
                                <EndDate>09/30/2005</EndDate>
                            </PeriodNumber>
                        </PeriodType>
                    </Branch>
                </root>'


========================
Scott Klein
Author of:
Professional SQL Server 2005 XML
Professional WCF Programming: .NET Development with the Windows Communication Foundation
Professional LINQ
========================
__________________
========================
Scott Klein
Author of:
Professional SQL Server 2005 XML
Professional WCF Programming: .NET Development with the Windows Communication Foundation
Professional LINQ
========================
 
Old June 25th, 2008, 11:29 AM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Figured it out:

x.value('../../@id[1]' ,'int')

I really need to use XML more often so I don't keep forgetting it...

========================
Scott Klein
Author of:
Professional SQL Server 2005 XML
Professional WCF Programming: .NET Development with the Windows Communication Foundation
Professional LINQ
========================





Similar Threads
Thread Thread Starter Forum Replies Last Post
XML question on div jastao XSLT 6 June 6th, 2008 05:24 PM
Convert XML to XML template question bonekrusher XSLT 3 July 12th, 2007 07:47 AM
convert XML to XMl - Another Namespace question bonekrusher XSLT 2 July 10th, 2007 07:32 AM
XML application question cutnedge XML 1 April 13th, 2007 04:19 PM
Question for .NET GURUs mike_abc Pro VB.NET 2002/2003 1 January 12th, 2005 10:43 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.