I am new to XML stuff. I've figured out how to query and return the values from the XML file (example below). However, I run into a problem that it only capture the first node of 'SerialNo' tag because the tag has the same node name "SerialNo" repeated. In the XML file, it has 4 serial numbers for SKU#TT234343, but it only gives me the first Serial11111. I am totally stuck and don't know how to list all of those serial#.
I would like the query result for SKU#TT234343, listing all 4 serial numbers if possible.
Please help. Thanks!
The XML File looks like:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<ROOT>
<ShipNotice version="1" >
<InvoiceDate>01/01/2015</InvoiceDate>
<InvoiceNumber>6868686</InvoiceNumber>
<ShipDate>02/02/2015</ShipDate>
<ShipTime>2306</ShipTime>
<PONumber>P444444</PONumber>
<PODate>03/03/2015</PODate>
<ShipCode>XXX</ShipCode>
<ShipDescription>FedEx Economy</ShipDescription>
<ShipTo>
<AddressName>ShipABC</AddressName>
<AddressContact>Name1</AddressContact>
<AddressLine1>2222 Street Name</AddressLine1>
<AddressLine2> </AddressLine2>
<City>AUSTIN</City>
<State>TX</State>
<ZipCode>78111</ZipCode>
</ShipTo>
<BillTo>
<AddressName>BillABC</AddressName>
<AddressContact>Name1</AddressContact>
<AddressLine1>1234 Street Name</AddressLine1>
<AddressLine2>-SUITE 111</AddressLine2>
<City>Los Angeles</City>
<State>CA</State>
<ZipCode>95136</ZipCode>
</BillTo>
<TotalWeight>324</TotalWeight>
<EmptyCartonWGT>0</EmptyCartonWGT>
<NumberOfCarton>1</NumberOfCarton>
<DirectShipFlag>D</DirectShipFlag>
<ShipFromWarehouse>88</ShipFromWarehouse>
<ShipFromZip>94538</ShipFromZip>
<ShipTrackNo>33333333</ShipTrackNo>
<EndUserPONumber>55555555</EndUserPONumber>
<CustomerSONumber/>
<Package sequence="1" >
<TrackNumber>666666666</TrackNumber>
<PackageWeight>324</PackageWeight>
<Item sequence="1" >
<SOLineNo>1</SOLineNo>
<MfgPN>XYZ1111111</MfgPN>
<SKU>TT234343</SKU>
<ShipQuantity>4</ShipQuantity>
<CustPOLineNo>1</CustPOLineNo>
<CustSOLineNo/>
<Description>Server1234</Description>
<CustPN/>
<UPC/>
<UnitPrice>1000</UnitPrice>
<EndUserPOLineNo>0</EndUserPOLineNo>
<SerialNo>Serial11111</SerialNo>
<SerialNo>Serial22222</SerialNo>
<SerialNo>Serial33333</SerialNo>
<SerialNo>Serial44444</SerialNo>
</Item>
<Item sequence="2" >
<SOLineNo>2</SOLineNo>
<MfgPN>XYZ222222</MfgPN>
<SKU>TT8848788</SKU>
<ShipQuantity>4</ShipQuantity>
<CustPOLineNo>2</CustPOLineNo>
<CustSOLineNo/>
<Description>GGG localization</Description>
<CustPN/>
<UPC/>
<UnitPrice>0.00</UnitPrice>
<EndUserPOLineNo>0</EndUserPOLineNo>
<SerialNo/>
</Item>
</Package>
</ShipNotice>
</ROOT>
The SQL Query:
DECLARE #XML AS XML, #hDoc AS INT, #SQL NVARCHAR (MAX)
EXEC sp_xml_preparedocument #hDoc OUTPUT, #xmlData
SELECT
InvoiceNumber, PONumber, PODate
, AddressName
, MfgPN, SerialNo
--, AddressContact, AddressLine1, AddressLine2, City, State, ZipCode
FROM OPENXML(#hDoc, '/ROOT/ShipNotice/Package/Item')
WITH
(
--- ################# Level 1 #################
InvoiceNumber [varchar](50) '../../InvoiceNumber',
PONumber [varchar](100) '../../PONumber',
PODate [varchar](100) '../../PODate',
--- ################# Level 2 #################
AddressName [varchar](100) '../../ShipTo/AddressName',
--- ################# Level 3 #################
MfgPN [varchar](100) 'MfgPN',
SerialNo [varchar](100) 'SerialNo'
)
You can try using the newer technology XQuery instead of OPENXML(). Using XQuery, you can use nodes() method to shred the XML on elements that will correspond to the rows in the output, and use value() to extract the element value :
SELECT
shipnotice.value('InvoiceNumber[1]','varchar(20)') InvoiceNumber
, shipnotice.value('PONumber[1]','varchar(20)') PONumber
, shipnotice.value('PODate[1]','varchar(20)') PODate
, shipnotice.value('(ShipTo/AddressName)[1]','varchar(100)') AddressName
, item.value('MfgPN[1]','varchar(100)') MfgPN
, serialno.value('.','varchar(100)') SerialNo
FROM #XML.nodes('/ROOT/ShipNotice') as t(shipnotice)
OUTER APPLY shipnotice.nodes('Package/Item') as t2(item)
OUTER APPLY item.nodes('SerialNo') as t3(serialno)
Sqlfiddle Demo
output :
| InvoiceNumber | PONumber | PODate | AddressName | MfgPN | SerialNo |
|---------------|----------|------------|-------------|------------|-------------|
| 6868686 | P444444 | 03/03/2015 | ShipABC | XYZ1111111 | Serial11111 |
| 6868686 | P444444 | 03/03/2015 | ShipABC | XYZ1111111 | Serial22222 |
| 6868686 | P444444 | 03/03/2015 | ShipABC | XYZ1111111 | Serial33333 |
| 6868686 | P444444 | 03/03/2015 | ShipABC | XYZ1111111 | Serial44444 |
| 6868686 | P444444 | 03/03/2015 | ShipABC | XYZ222222 | |
Related
I am using SQL Server 2014 and I have a table (t1) in my database which contain a column called "MealPlan".
This column contains a list of strings (extract below):
MealPlan
Sansrepas315€/pers.=630€pour2pers.Devis/RésaSelectionner
Sansrepas394€/pers.=787€pour2pers.Devis/RésaSelectionner
Sansrepas547€/pers.=1 093€pour2pers.Devis/RésaSelectionner
Sansrepas547€/pers.=1 093€pour2pers.Devis/RésaSelectionner
Sansrepas700€/pers.=1 400€pour2pers.Devis/RésaSelectionner
Sansrepas328€/pers.=656€pour2pers.Devis/RésaSelectionner
I need to extract the numbers between the characters = and €
I have the following codes in place which does exactly what I need:
SUBSTRING(MealPlan,LEN(LEFT(MealPlan,CHARINDEX('=', MealPlan)+1)),LEN(MealPlan) - LEN(LEFT(MealPlan,CHARINDEX('=', MealPlan))) - LEN(RIGHT(MealPlan,CHARINDEX('€', (REVERSE(MealPlan)))))) AS [Price]
After running the above my column "Price" appear as follows:
Price
630
787
1 093
1 093
1 400
656
However, I want to get rid of that space in the numbers where a thousand digit is present.
My expected output:
Price
630
787
1093
1093
1400
656
I have tried the following but it is not working:
REPLACE(SUBSTRING(MealPlan,LEN(LEFT(MealPlan,CHARINDEX('=', MealPlan)+1)),LEN(MealPlan) - LEN(LEFT(MealPlan,CHARINDEX('=', MealPlan))) - LEN(RIGHT(MealPlan,CHARINDEX('€', (REVERSE(MealPlan)))))), ' ','') AS [Price2]
Any help would be much appreciated.
I just ran your query in my sample database and it is working fine..
select
REPLACE(SUBSTRING(Description,
LEN(LEFT(Description,CHARINDEX('=', Description)+1)),
LEN(Description) - LEN(LEFT(Description,CHARINDEX('=', Description))) - LEN(RIGHT(Description,CHARINDEX('€', (REVERSE(Description)))))
), ' ','') AS [Description]
from Worker
Table #a1
| MealPlan |
| -------- |
| Sansrepas315€/pers.=630€pour2pers.Devis/RésaSelectionner |
| Sansrepas394€/pers.=787€pour2pers.Devis/RésaSelectionner |
| Sansrepas547€/pers.=1 093€pour2pers.Devis/RésaSelectionner |
Query
SELECT
REPLACE(
SUBSTRING(MealPlan,CHARINDEX('=', MealPlan)+1, CHARINDEX('=',REVERSE(MealPlan)) - CHARINDEX('€',REVERSE(MealPlan)) -1 )
,' ', ''
)
as value
FROM #a1
results
value
630
787
1093
The solution about "cut and paste" provided by Jiří Baum above did the trick for me.
It is very easy to tokenize a string of characters by using XML and XQuery.
No need to parse string and call multiple functions: SUBSTRING(), CHARINDEX(), PATINDEX(), LEN(), REVERSE(), etc.
SQL
-- DDL and sample data population, start
DECLARE #tbl TABLE (ID INT IDENTITY(1,1) PRIMARY KEY, MealPlan NVARCHAR(1000));
INSERT INTO #tbl (MealPlan) VALUES
(N'Sansrepas315€/pers.=630€pour2pers.Devis/RésaSelectionner'),
(N'Sansrepas394€/pers.=787€pour2pers.Devis/RésaSelectionner'),
(N'Sansrepas547€/pers.=1 093€pour2pers.Devis/RésaSelectionner'),
(N'Sansrepas547€/pers.=1 093€pour2pers.Devis/RésaSelectionner'),
(N'Sansrepas700€/pers.=1 400€pour2pers.Devis/RésaSelectionner'),
(N'Sansrepas328€/pers.=656€pour2pers.Devis/RésaSelectionner');
-- DDL and sample data population, end
DECLARE #separator CHAR(1) = '='
, #euro CHAR(1) = '€';
SELECT t.*
, REPLACE(c.value('(/root/r[3]/text())[1]', 'VARCHAR(20)'),SPACE(1),'') AS Price
FROM #tbl AS t
CROSS APPLY (SELECT TRY_CAST('<root><r><![CDATA[' +
REPLACE(REPLACE(MealPlan,#euro,#separator), #separator, ']]></r><r><![CDATA[') +
']]></r></root>' AS XML)) AS t1(c);
Output
+----+------------------------------------------------------------+--------+
| ID | MealPlan | Result |
+----+------------------------------------------------------------+--------+
| 1 | Sansrepas315€/pers.=630€pour2pers.Devis/RésaSelectionner | 630 |
| 2 | Sansrepas394€/pers.=787€pour2pers.Devis/RésaSelectionner | 787 |
| 3 | Sansrepas547€/pers.=1 093€pour2pers.Devis/RésaSelectionner | 1093 |
| 4 | Sansrepas547€/pers.=1 093€pour2pers.Devis/RésaSelectionner | 1093 |
| 5 | Sansrepas700€/pers.=1 400€pour2pers.Devis/RésaSelectionner | 1400 |
| 6 | Sansrepas328€/pers.=656€pour2pers.Devis/RésaSelectionner | 656 |
+----+------------------------------------------------------------+--------+
Try this:
Declare #MealPlan as varchar(250) = 'Sansrepas547€/pers.=1 093€pour2pers.Devis/RésaSelectionner'
Select Replace(
SUBSTRING(#MealPlan,
CharIndex('=', #MealPlan)+1,
CharIndex(')',Replace(#MealPlan, '€p', ')'))- CharIndex('=', #MealPlan)-1 ),
' ', '')
Output:
Let me know if this doesn't work or you have a problem understanding the query.
I'm trying to extract XML data from feedback table consist of message column; below is the XML code:
<?xml version="1.0" encoding="UTF-8"?>
<message>Hi Hello</message>
<channel>XYZ</channel>
<sentiment-score>5.0</sentiment-score>
<structured-fields>
<structured-filed><name>NA_score</name><value>5</value></structured-filed>
<structured-filed><name>NPS_score</name><value>10</value></structured-filed>
<structured-fields>
I need to extract the above XML code from message column.
Your XML document needs to have a single XML element as the document root:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<message>Hi Hello</message>
<channel>XYZ</channel>
<sentiment-score>5.0</sentiment-score>
<structured-fields>
<structured-field><name>NA_score</name><value>5</value></structured-field>
<structured-field><name>NPS_score</name><value>10</value></structured-field>
</structured-fields>
</root>
Once that is fixed then just use an XMLTable and XPath:
SELECT *
FROM XMLTABLE(
'/root'
PASSING XMLType( '<?xml version="1.0" encoding="UTF-8"?><root><message>Hi Hello</message><channel>XYZ</channel><sentiment-score>5.0</sentiment-score><structured-fields><structured-field><name>NA_score</name><value>5</value></structured-field><structured-field><name>NPS_score</name><value>10</value></structured-field></structured-fields></root>' )
COLUMNS
MESSAGE VARCHAR2(200) PATH '//message',
CHANNEL VARCHAR2(200) PATH '//channel',
SENTIMENT_SCORE NUMBER(3,1) PATH '//sentiment-score',
NA_SCORE NUMBER(3,0) PATH '//structured-fields/structured-field[name/text()="NA_score"]/value',
NPS_SCORE NUMBER(3,0) PATH '//structured-fields/structured-field[name/text()="NPS_score"]/value'
);
Output:
MESSAGE | CHANNEL | SENTIMENT_SCORE | NA_SCORE | NPS_SCORE
:------- | :------ | --------------: | -------: | --------:
Hi Hello | XYZ | 5 | 5 | 10
db<>fiddle here
I have a table List shown below:
+------+-------------------------------------+
| Code | name |
+------+-------------------------------------+
| A001 | ABBOTT_1000000 |
| A002 | AGCO_1000001 |
| A003 | ALFA LAVAL_1000002 |
| A004 | ALSTOM POWER INDIA LIMITED_1000003 |
| A005 | AMERICAN BUREAU OF SHIPPING_1000004 |
+------+-------------------------------------+
I need to update the table extracting the characters present after _ in name and replace them in code column. like this.
+---------+-------------------------------------+
| Code | name |
+---------+-------------------------------------+
| 1000000 | ABBOTT_1000000 |
| 1000001 | AGCO_1000001 |
| 1000002 | ALFA LAVAL_1000002 |
| 1000003 | ALSTOM POWER INDIA LIMITED_1000003 |
| 1000004 | AMERICAN BUREAU OF SHIPPING_1000004 |
+---------+-------------------------------------+
This is has to be done in sql server 2012. please help me.
Try this
with cte as
(
select substring(name,charindex('_',name)+1,len(name)) as ext_str,*
from yourtable
)
update cte set code = ext_str
You can try to use SUBSTRING in following:
SAMPLE DATA
CREATE TABLE #MyTable
(
Code NVARCHAR(60),
Name NVARCHAR(60)
)
INSERT INTO #MyTable VALUES
('A001','ABBOTT_1000000' ),
('A002','AGCO_1000001' ),
('A003','ALFA LAVAL_1000002' ),
('A004','ALSTOM POWER INDIA LIMITED_1000003' ),
('A005','AMERICAN BUREAU OF SHIPPING_1000004' )
QUERY
UPDATE #MyTable
SET Code = SUBSTRING(Name, CHARINDEX('_', Name) + 1, LEN(Name))
TESTING
SELECT * FROM #MyTable
DROP TABLE #MyTable
OUTPUT
Code Name
1000000 ABBOTT_1000000
1000001 AGCO_1000001
1000002 ALFA LAVAL_1000002
1000003 ALSTOM POWER INDIA LIMITED_1000003
1000004 AMERICAN BUREAU OF SHIPPING_1000004
SQL FIDDLE
DEMO
UPDATE <table>
SET name = STUFF(name, 1, CHARINDEX('_', name), '')
WHERE name like '%[_]%'
You can do this:
First, you select the number that you want to place as code
select substr(name,-1,7) from table_name
After this, you can update the table.So,The complete statement will be :
update table_name t set t.code = (select substr(name,-1,7) from table_name where code = t.code);
You can use RIGHT together with CHARINDEX:
SQL Fiddle
UPDATE tbl
SET Code = RIGHT(Name, LEN(Name) - CHARINDEX('_', Name))
WHERE CHARINDEX('_', Name) > 0
I have a XML file, which I need to import into a SQL Server database. The XML file is build like this:
<report>
<deltagere>
<deltager>
<number>142555267</number>
<date>29-12-2006</date>
<name>
<name>
<from>01-05-2000</from>
<to>01-01-2003</to>
<text>foo</text>
</name>
<name>
<from>01-01-2003</from>
<to>29-12-2006</to>
<text>bzz</text>
</name>
</name>
<information>
<deltagertype>person</deltagertype>
<leader>John Smith</leader>
<status>Active</status>
</information>
<role>Responsible</role>
</deltager>
<deltager>
<number>4000134982</number>
<date>05-12-2007</date>
<name>
<name>
<from>07-07-2007</from>
<to>05-12-2007</to>
<text>bar</text>
</name>
</name>
<information>
<deltagertype>person</deltagertype>
<leader>Wolfgang Smith</leader>
<status>Active</status>
</information>
<role>Responsible</role>
</deltager>
...
</deltagere>
</report>
As you can see the name attribute can hold multiple names. I have managed to import the XML into my database, but only with the first name attribute.
The code I have written so far is:
DECLARE #XmlFile XML
SELECT #XmlFile = BulkColumn
FROM OPENROWSET(BULK 'C:\input.xml', SINGLE_BLOB) x;
INSERT INTO dbo.deltagere(number, dato, nameFrom, nameTo, nameText, deltagertype, leader, deltagerStatus, deltagerRole)
SELECT
number = deltagere.value('(number)[1]', 'bigint'),
dato = deltagere.value('(date)[1]', 'varchar(10)'),
nameFrom = deltagere.value('(name/name/from)[1]', 'varchar(10)'),
nameTo = deltagere.value('(name/name/to)[1]', 'varchar(10)'),
nameText = deltagere.value('(name/name/text)[1]', 'varchar(30)'),
deltagertype = deltagere.value('(information/deltagertype)[1]', 'varchar(20)'),
leader = deltagere.value('(information/leader)[1]', 'varchar(50)'),
deltagerStatus = deltagere.value('(information/status)[1]', 'varchar(50)'),
deltagerRole = deltagere.value('(role)[1]', 'varchar(50)')
FROM
#XmlFile.nodes('/report/deltagere/deltager') AS XTbl(deltagere);
Which gives me this output:
| number | dato | nameFrom | nameTo | nameText | deltagertype | ...
| 142555267 | 29-12-2006 | 01-05-2000 | 01-01-2003 | foo | person | ...
| 4000134982 | 05-12-2007 | 07-07-2007 | 05-12-2007 | bar | person | ...
I would like to have a row for each name/name. So something like this:
-------------------------------------------------------
| number | dato | nameFrom | nameTo | nameText | deltagertype | ...
| 142555267 | 29-12-2006 | 01-05-2000 | 01-01-2003 | foo | person | ...
| 142555267 | 29-12-2006 | 01-01-2003 | 29-12-2006 | bzz | person | ...
| 4000134982 | 05-12-2007 | 07-07-2007 | 05-12-2007 | bar | person | ...
and so on.
I'm really lost in how to do this. So I hope any of you have any ideas on how to modify my code to allow this or maybe an different approach on the problem.
Try this - you need to do a second .nodes() call to enumerate all <name> subnodes:
SELECT
number = deltagere.value('(number)[1]', 'bigint'),
dato = deltagere.value('(date)[1]', 'varchar(10)'),
-- NEW NEW NEW - read from `XC` pseudo columns to get 1-n names
nameFrom = XC.value('(from)[1]', 'varchar(10)'),
nameTo = XC.value('(to)[1]', 'varchar(10)'),
nameText = XC.value('(text)[1]', 'varchar(30)'),
deltagertype = deltagere.value('(information/deltagertype)[1]', 'varchar(20)'),
leader = deltagere.value('(information/leader)[1]', 'varchar(50)'),
deltagerStatus = deltagere.value('(information/status)[1]', 'varchar(50)'),
deltagerRole = deltagere.value('(role)[1]', 'varchar(50)')
FROM
#XmlFile.nodes('/report/deltagere/deltager') AS XTbl(deltagere)
CROSS APPLY
deltagere.nodes('name/name') AS XT2(XC)
I am trying to learn XQuery and Xpath in SQL Server
I created a sample file and uploaded it to a Table with 2 columns ID, XMLDoc. The below code is within the document in the XMLDoc column so it is the only record in the column.
I am trying to query the file so it will show all the results in a table like a normal select statement would. How would you construct the select statement to select all the information like a select * ? How would you select one field like all suppliers? I would like to select the supplier, requestor for each item.
Here is the xml:
<tst:Document xmlns:tst ="http://www.w3.org/2001/XMLSchema" SchemaVersion="0.1" Classification="Test" UniqueIdentifier="1234" Title="Test">
<tst:Revision RevNumber="0" TimeStamp="2013-01-21T12:56:00">
<tst:Author Name="Me" Guid="1234" />
</tst:Revision>
<tst:Formats>
<tst:A12 Item="1">
<tst:Requestor Name="ADC" />
<tst:Supplier Name="BBC" />
<tst:Code>B</tst:Code>
<tst:IsRequirement>true</tst:IsRequirement>
<tst:IsNotRequired>false</tst:IsInformation>
<tst:Remarks>ADC (Random Input Section)</tst:Remarks>
<tst:Notes>Next Round.</tst:Notes>
<tst:Events>
<tst:SubTest Item="0">
<tst:BLDG>BLDG1</tst:BLDG>
<tst:BLDG2>BLDG2</tst:BLDG2>
<tst:Function>Testing</tst:Function>
<tst:Desciption>Normal Flow</tst:Desciption>
</tst:SubTest>
</tst:Events>
<tst:IsReady>true</tst:IsReady>
<tst:IsNotReady>false</tst:IsNotReady>
</tst:A12>
<tst:A12 Item="2">
<tst:Requestor Name="ADC" />
<tst:Supplier Name="BBC" />
<tst:Code>A</tst:Code>
<tst:IsRequirement>true</tst:IsRequirement>
<tst:IsInformation>false</tst:IsInformation>
<tst:Remarks>Requirement Not yet met.</tst:Remarks>
<tst:Notes>Ready.</tst:Notes>
<tst:Events>
<tst:SubTest Item="0">
<tst:BLDG>BLDG3</tst:BLDG>
<tst:BLDG2>BLDG4</tst:BLDG2>
<tst:TotalEvents>1</tst:TotalEvents>
<tst:Function>Development</tst:Function>
<tst:Desciption>Process Flow</tst:Desciption>
</tst:SubTest>
</tst:Events>
<tst:IsReady>true</tst:IsReady>
<tst:IsNotReady>false</tst:IsNotReady>
</tst:A12>
</tst:Formats>
</tst:Document>
Query I ran
I just got a return, but it is still showing it in xml form:
Select XMLDoc.query('/*/*/*/*[local-name()=("Requestor", "Supplier")]')
From XMLLoad
I Updated the xml snippet, sry had a typo! It will load now
INSERT INTO TableName(ColumnName)
SELECT * FROM OPENROWSET(
BULK 'C:\Users\Filepath.xml',
SINGLE_BLOB) AS x;
SQL Fiddle
MS SQL Server 2008 Schema Setup:
create table XMLDoc (XMLLoad xml);
insert into XMLDoc(XMLLoad) values('
<tst:Document xmlns:tst ="http://www.w3.org/2001/XMLSchema" SchemaVersion="0.1" Classification="Test" UniqueIdentifier="1234" Title="Test">
<tst:Revision RevNumber="0" TimeStamp="2013-01-21T12:56:00">
<tst:Author Name="Me" Guid="1234" />
</tst:Revision>
<tst:Formats>
<tst:A12 Item="1">
<tst:Requestor Name="ADC" />
<tst:Supplier Name="BBC" />
<tst:Code>B</tst:Code>
<tst:IsRequirement>true</tst:IsRequirement>
<tst:IsInformation>false</tst:IsInformation>
<tst:Remarks>ADC (Random Input Section)</tst:Remarks>
<tst:Notes>Next Round.</tst:Notes>
<tst:Events>
<tst:SubTest Item="0">
<tst:BLDG>BLDG1</tst:BLDG>
<tst:BLDG2>BLDG2</tst:BLDG2>
<tst:Function>Testing</tst:Function>
<tst:Desciption>Normal Flow</tst:Desciption>
</tst:SubTest>
</tst:Events>
<tst:IsReady>true</tst:IsReady>
<tst:IsNotReady>false</tst:IsNotReady>
</tst:A12>
<tst:A12 Item="2">
<tst:Requestor Name="ADC" />
<tst:Supplier Name="BBC" />
<tst:Code>A</tst:Code>
<tst:IsRequirement>true</tst:IsRequirement>
<tst:IsInformation>false</tst:IsInformation>
<tst:Remarks>Requirement Not yet met.</tst:Remarks>
<tst:Notes>Ready.</tst:Notes>
<tst:Events>
<tst:SubTest Item="0">
<tst:BLDG>BLDG3</tst:BLDG>
<tst:BLDG2>BLDG4</tst:BLDG2>
<tst:TotalEvents>1</tst:TotalEvents>
<tst:Function>Development</tst:Function>
<tst:Desciption>Process Flow</tst:Desciption>
</tst:SubTest>
</tst:Events>
<tst:IsReady>true</tst:IsReady>
<tst:IsNotReady>false</tst:IsNotReady>
</tst:A12>
</tst:Formats>
</tst:Document>');
Query 1:
with xmlnamespaces('http://www.w3.org/2001/XMLSchema' as tst)
select A12.X.value('#Item', 'int') as A12,
A12.X.value('tst:Requestor[1]/#Name', 'varchar(25)') as Requestor,
A12.X.value('tst:Supplier[1]/#Name', 'varchar(25)') as Supplier,
A12.X.value('(tst:Code/text())[1]', 'varchar(25)') as Code,
A12.X.value('(tst:IsRequirement/text())[1]', 'bit') as IsRequirement,
A12.X.value('(tst:IsInformation/text())[1]', 'bit') as IsInformation,
A12.X.value('(tst:Remarks/text())[1]', 'varchar(50)') as Remarks,
A12.X.value('(tst:Notes/text())[1]', 'varchar(50)') as Notes,
ST.X.value('#Item', 'int') as SubTest,
ST.X.value('(tst:BLDG/text())[1]', 'varchar(25)') as BLDG,
ST.X.value('(tst:BLDG2/text())[1]', 'varchar(25)') as BLDG2,
ST.X.value('(tst:TotalEvents/text())[1]', 'int') as TotalEvents,
ST.X.value('(tst:Function/text())[1]', 'varchar(25)') as [Function],
ST.X.value('(tst:Desciption/text())[1]', 'varchar(50)') as Desciption
from XMLDoc as X
cross apply X.XMLLoad.nodes('/tst:Document/tst:Formats/tst:A12') as A12(X)
cross apply A12.X.nodes('tst:Events/tst:SubTest') as ST(X)
Results:
| A12 | REQUESTOR | SUPPLIER | CODE | ISREQUIREMENT | ISINFORMATION | REMARKS | NOTES | SUBTEST | BLDG | BLDG2 | TOTALEVENTS | FUNCTION | DESCIPTION |
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 1 | ADC | BBC | B | 1 | 0 | ADC (Random Input Section) | Next Round. | 0 | BLDG1 | BLDG2 | (null) | Testing | Normal Flow |
| 2 | ADC | BBC | A | 1 | 0 | Requirement Not yet met. | Ready. | 0 | BLDG3 | BLDG4 | 1 | Development | Process Flow |
Check out value() and nodes().