SQLUSA.com
SQL SERVER 2008 GRAND SLAM
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL JOBS SQL Server Training Scripts JOB BANK
  SQL Server 2008 Training Scripts  
  SQL Server 2005 Training Scripts  
News SQL Server Articles SQL Format

How to use the XML value method?

Execute the following SQL Server T-SQL example scripts in Management Studio Query Editor to demonstrate the xml value method.

-- List all banks used by bike stores

USE AdventureWorks;

-- XML value method

SELECT distinct Demographics.value('declare namespace

AWMI="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/StoreSurvey";

(AWMI:StoreSurvey/AWMI:BankName)[1]','varchar(30)')

as [Store Banks]

FROM Sales.Store

GO

/* Results

 

Store Banks

Guardian Bank

International Bank

International Security

Primary Bank & Reserve

Primary International

Reserve Security

United Security

*/

------------

 

-- List sales order ids and total order amounts

USE AdventureWorks

GO

DECLARE  @xml XML

SELECT @xml = (SELECT SalesOrderID,

                      SubTotal

               FROM   Sales.SalesOrderHeader

               FOR XML RAW('Row'),ROOT('Rows'),ELEMENTS)

— XML value method

SELECT   SalesOrderID = list.SO.value('SalesOrderID[1]','nvarchar(10)'),

         TotalDollar = list.SO.value('SubTotal[1]','nvarchar(16)')

FROM     @xml.nodes('/Rows/Row') AS list (SO)

ORDER BY SalesOrderID DESC

GO

/* Partial results

 

SalesOrderID      TotalDollar

75123             189.9700

75122             30.9700

75121             74.9800

75120             84.9600

75119             42.2800

75118             135.2300

*/

------------

 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Microsoft SQL Server 2000 Training Videos at www.sqlusa.com.

FREE SQL & Business Intelligence / OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011
Invest in Your SUCCESS!

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts


Copyright 2005-2011, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.