TSQL remove characters after a string - sql

I have a table column containing set of addresses as XML data:
<?xml version="1.0" encoding="utf-16"?>
<AddressBook>
<Address>
<ID>-1</ID>
<AddressID>PRIMARY</AddressID>
<FirstName>JOHN L.</FirstName>
<LastName>JOHSON/JOHN L.</LastName>
<StreetLine1>123 NOWHERE ST</StreetLine1>
<City>OVERHERE</City>
<StateName>CA</StateName>
<StateCode>5</StateCode>
<PostalCode>12345</PostalCode>
<CountryName>United States</CountryName>
<CountryCode>en-US</CountryCode>
<PhoneNumber>5551231234</PhoneNumber>
<PhoneNumber2>5551231234</PhoneNumber2>
<TaxScheduleID>CA</TaxScheduleID>
<ShowAddress>1</ShowAddress>
</Address>
<Address>
<ID>-1</ID>
<AddressID>SECONDARY</AddressID>
<FirstName>SAM</FirstName>
<LastName>LARSON/SAM</LastName>
<StreetLine1>456 OVERHERE</StreetLine1>
<City>NOTTHERE</City>
<StateName>CA</StateName>
<StateCode>5</StateCode>
<PostalCode>54321</PostalCode>
<CountryName>United States</CountryName>
<CountryCode>en-US</CountryCode>
<PhoneNumber>5553334444</PhoneNumber>
<PhoneNumber2>5553334444</PhoneNumber2>
<TaxScheduleID>CA</TaxScheduleID>
<ShowAddress>1</ShowAddress>
</Address>
</AddressBook>
My issues is that the last name element contains an unnecessary duplicate of the first named preceding with '/' :
<FirstName>JOHN L.</FirstName>
<LastName>JOHSON/JOHN L.</LastName>
How can I remove the '/' and every character following '/' in the last name elements only to create an outcome as such?
<FirstName>JOHN L.</FirstName>
<LastName>JOHSON</LastName>

Try using charindex :
Select Left('JOHSON/JOHN L.', CHARINDEX('/', 'JOHSON/JOHN L.') - 1)

Fully working sample with table variable containing 2 records:
declare #t table (xmldata xml);
insert #t values ('
<AddressBook>
<Address>
<ID>-1</ID>
<AddressID>PRIMARY</AddressID>
<FirstName>JOHN L.</FirstName>
<LastName>JOHSON/JOHN L.</LastName>
<StreetLine1>123 NOWHERE ST</StreetLine1>
<City>OVERHERE</City>
<StateName>CA</StateName>
<StateCode>5</StateCode>
<PostalCode>12345</PostalCode>
<CountryName>United States</CountryName>
<CountryCode>en-US</CountryCode>
<PhoneNumber>5551231234</PhoneNumber>
<PhoneNumber2>5551231234</PhoneNumber2>
<TaxScheduleID>CA</TaxScheduleID>
<ShowAddress>1</ShowAddress>
</Address>
<Address>
<ID>-1</ID>
<AddressID>SECONDARY</AddressID>
<FirstName>SAM</FirstName>
<LastName>LARSON/SAM</LastName>
<StreetLine1>456 OVERHERE</StreetLine1>
<City>NOTTHERE</City>
<StateName>CA</StateName>
<StateCode>5</StateCode>
<PostalCode>54321</PostalCode>
<CountryName>United States</CountryName>
<CountryCode>en-US</CountryCode>
<PhoneNumber>5553334444</PhoneNumber>
<PhoneNumber2>5553334444</PhoneNumber2>
<TaxScheduleID>CA</TaxScheduleID>
<ShowAddress>1</ShowAddress>
</Address>
</AddressBook>');
insert #t values ('
<AddressBook>
<Address>
<ID>-1</ID>
<AddressID>PRIMARY</AddressID>
<FirstName>JANE L.</FirstName>
<LastName>JOHSON/JANE L.</LastName>
<StreetLine1>123 NOWHERE ST</StreetLine1>
<City>OVERHERE</City>
<StateName>CA</StateName>
<StateCode>5</StateCode>
<PostalCode>12345</PostalCode>
<CountryName>United States</CountryName>
<CountryCode>en-US</CountryCode>
<PhoneNumber>5551231234</PhoneNumber>
<PhoneNumber2>5551231234</PhoneNumber2>
<TaxScheduleID>CA</TaxScheduleID>
<ShowAddress>1</ShowAddress>
</Address>
<Address>
<ID>-1</ID>
<AddressID>SECONDARY</AddressID>
<FirstName>NIGEL</FirstName>
<LastName>NIGEL JR./NIGEL</LastName>
<StreetLine1>456 OVERHERE</StreetLine1>
<City>NOTTHERE</City>
<StateName>CA</StateName>
<StateCode>5</StateCode>
<PostalCode>54321</PostalCode>
<CountryName>United States</CountryName>
<CountryCode>en-US</CountryCode>
<PhoneNumber>5553334444</PhoneNumber>
<PhoneNumber2>5553334444</PhoneNumber2>
<TaxScheduleID>CA</TaxScheduleID>
<ShowAddress>1</ShowAddress>
</Address>
</AddressBook>');
update t
set xmldata =
(select n.a.value('ID[1]','nvarchar(max)') ID,
n.a.value('AddressID[1]','nvarchar(max)') AddressID,
n.a.value('FirstName[1]','nvarchar(max)') FirstName,
LEFT(n.a.value('LastName[1]','nvarchar(max)'), LEN(n.a.value('LastName[1]','nvarchar(max)'))
-1-LEN(n.a.value('FirstName[1]','nvarchar(max)'))) LastName,
n.a.value('StreetLine1[1]','nvarchar(max)') StreetLine1,
n.a.value('City[1]','nvarchar(max)') City,
n.a.value('StateName[1]','nvarchar(max)') StateName,
n.a.value('StateCode[1]','nvarchar(max)') StateCode,
n.a.value('PostalCode[1]','nvarchar(max)') PostalCode,
n.a.value('CountryName[1]','nvarchar(max)') CountryName,
n.a.value('CountryCode[1]','nvarchar(max)') CountryCode,
n.a.value('PhoneNumber[1]','nvarchar(max)') PhoneNumber,
n.a.value('PhoneNumber2[1]','nvarchar(max)') PhoneNumber2,
n.a.value('TaxScheduleID[1]','nvarchar(max)') TaxScheduleID,
n.a.value('ShowAddress[1]','nvarchar(max)') ShowAddress
from AddressBook.nodes('Address') n(a)
for xml path('Address'), root('AddressBook'), type
)
from #t t
cross apply xmldata.nodes('AddressBook') ab(AddressBook);
select *
from #t;

Related

Stuck in T-SQL : XML to temp table

I have a little challenge where I am currently stuck in.
This is my query:
DECLARE #xml XML
SET #xml =
'<beginmsg>
<refmsg>A1234567</refmsg>
<shipments>
<shipment>
<load>
<address>
<name>Loadname</name>
<street>Street</street>
<zipcode>ABCDE</zipcode>
<city>Munchen</city>
<countrycode>DE</countrycode>
</address>
<dateTime>
<date>2021-02-02</date>
<timeFrom>08:00:00</timeFrom>
</dateTime>
</load>
<unload>
<address>
<name>unloadname</name>
<street>Street unload</street>
<zipcode>1111</zipcode>
<city>Dresden</city>
<countrycode>DE</countrycode>
</address>
</unload>
<goods>
<good>
<description>Cookies</description>
<quantity>1</quantity>
</good>
<good>
<description>Cookies</description>
<quantity>3</quantity>
</good>
<good>
<description>Some food</description>
<quantity>2</quantity>
</good>
</goods>
<barcodes>
<barcode>7829348742910092309325</barcode>
<barcode>7829348742112344114414</barcode>
<barcode>0984746166149566188446</barcode>
</barcodes>
<references>
<reference>GBP-4362</reference>
</references>
</shipment>
<shipment>
<load>
<address>
<name>shipment 2 load</name>
<street>Street load2</street>
<zipcode>1234 RR</zipcode>
<city>Koln</city>
<countrycode>DE</countrycode>
</address>
<dateTime>
<date>2021-03-03</date>
<timeFrom>10:00:00</timeFrom>
</dateTime>
</load>
<unload>
<address>
<name>Shipment 2 unload</name>
<street>Street 2 unl</street>
<zipcode>1000 AA</zipcode>
<city>Amsterdam</city>
<countrycode>NL</countrycode>
</address>
</unload>
<goods>
<good>
<description>Televisions</description>
<quantity>2</quantity>
</good>
</goods>
<barcodes>
<barcode>0984746166149566188446</barcode>
</barcodes>
<references>
<reference>HBR-7211CX</reference>
</references>
</shipment>
</shipments>
</beginmsg>'
IF OBJECT_ID('tempdb..#Goods') IS NOT NULL DROP TABLE #Goods;
CREATE TABLE #Goods
(
row INT,
shipmentreference VARCHAR(100),
description VARCHAR(MAX),
quantity INT,
barcode VARCHAR(MAX)
);
INSERT INTO #Goods
SELECT
"row" = ROW_NUMBER() OVER (PARTITION BY shipment.shipment ORDER BY shipment.shipment),
"shipmentreference" = CAST(shipment.shipment.value('(references/reference)[1]', 'VARCHAR(100)') AS VARCHAR(MAX)) + '-' + CAST(ROW_NUMBER() OVER (PARTITION BY shipment.shipment ORDER BY shipment.shipment) AS VARCHAR(MAX)),
"description" = goods.goods.value('(description)[1]', 'VARCHAR(10)'),
"quantity" = goods.goods.value('(quantity)[1]', 'VARCHAR(10)'),
"barcode" = barcode.barcode.value('(barcode)[1]', 'VARCHAR(100)')
FROM
#Xml.nodes('beginmsg/shipments/shipment') shipment(shipment)
CROSS APPLY
shipment.shipment.nodes('goods/good') goods(goods)
CROSS APPLY
shipment.shipment.nodes('barcodes') barcode(barcode)
SELECT * FROM #Goods
The XML contains 2 shipments.
In the 1st shipment there are 3 goods segment with each 1 'good' segment. In the 2nd shipment there is one good segment.
I would like to merge each good, with their information and with the 1st barcode for the 1st good, 2nd barcode for the 2nd good etc. etc.
So I was thinking to create a TEMP table #goods and a temp table to store the barcodes in. In both temp tables I would like to add the "reference" so I can join both tables on that unique field.
But at this moment I am stuck. In my final query (as shown above) the output is as follow:
row
reference
description
quantity
barcode
1
GBP-4362-1
Cookies
1
7829348742910092309325
2
GBP-4362-1
Cookies
3
7829348742910092309325
3
GBP-4362-1
Some food
2
7829348742910092309325
1
HBR-7211CX-1
Television
2
0984746166149566188446
So the barcode is each first (what is actually what I am querying :))
How can I iterate through these barcodes nodes and how can I manage to merge it into the row?

XML to SQL - SQL Server

I'm traversing an XML file to read nodes and fill into to SQL Server tables. I have a Root node having Department node which further may have one or more as element. I want to select all the possible values from in a SQL result set.
Please find below XML I'm referring:
DECLARE #x XML='
<Root>
<Department>
<DeptID>D101</DeptID>
<DeptID>D102</DeptID>
</Department>
</Root>'
I'm using below SQL Query to get the data from XML but I can read only first DeptID as I'm passing [1] inside DeptID[1]. If I pass [2] I can get thee second value. But in real life scenario, I won't be able to know how many DeptID would be there in the XML. So I want a generic script to read as many as DeptIDs comes in XML.
SELECT n.value('DeptID[1]','varchar(10)') AS DeptID FROM #x.nodes('/Root/Department') R(n)
You can use OpenXMl method of sql server to get more elements in table as follows.
Step 1: Suppose this is your sample XML data.
DECLARE #XML XML='
<ROOT>
<Customers>
<Customer CustomerID="C001" CustomerName="Arshad Ali">
<Orders>
<Order OrderID="10248" OrderDate="2012-07-04T00:00:00">
<OrderDetail ProductID="10" Quantity="5" />
<OrderDetail ProductID="11" Quantity="12" />
<OrderDetail ProductID="42" Quantity="10" />
</Order>
</Orders>
<Address> Address line 1, 2, 3</Address>
</Customer>
<Customer CustomerID="C002" CustomerName="Paul Henriot">
<Orders>
<Order OrderID="10245" OrderDate="2011-07-04T00:00:00">
<OrderDetail ProductID="11" Quantity="12" />
<OrderDetail ProductID="42" Quantity="10" />
</Order>
</Orders>
<Address> Address line 5, 6, 7</Address>
</Customer>
<Customer CustomerID="C003" CustomerName="Carlos Gonzlez">
<Orders>
<Order OrderID="10283" OrderDate="2012-08-16T00:00:00">
<OrderDetail ProductID="72" Quantity="3" />
</Order>
</Orders>
<Address> Address line 1, 4, 5</Address>
</Customer>
</Customers>
</ROOT>'
Step 2: Use of OPENXML method to get elements at any level as follows.
DECLARE #hDoc AS INT, #SQL NVARCHAR (MAX)
EXEC sp_xml_preparedocument #hDoc OUTPUT, #XML
SELECT CustomerID, CustomerName, Address, OrderID, OrderDate, ProductID, Quantity
FROM OPENXML(#hDoc, 'ROOT/Customers/Customer/Orders/Order/OrderDetail')
WITH
(
CustomerID [varchar](50) '../../../#CustomerID',
CustomerName [varchar](100) '../../../#CustomerName',
Address [varchar](100) '../../../Address',
OrderID [varchar](1000) '../#OrderID',
OrderDate datetime '../#OrderDate',
ProductID [varchar](50) '#ProductID',
Quantity int '#Quantity'
)
EXEC sp_xml_removedocument #hDoc
GO
Above steps will give you following Output.
Try it like this
DECLARE #x XML='
<Root>
<Department>
<DeptID>D101</DeptID>
<DeptID>D102</DeptID>
</Department>
</Root>';
SELECT d.value('text()[1]','varchar(10)') AS DeptID
FROM #x.nodes('/Root/Department/DeptID') A(d);
Your own code
SELECT n.value('DeptID[1]','varchar(10)') AS DeptID
FROM #x.nodes('/Root/Department') R(n)
... follows the right idea. But .nodes() must return the repeating element, which is <DeptID>. Your approach is looking for the first <DeptID> within <Department> actually

XML element value with width limitation

I am new to XML and trying to solve the following in SQL Server 2008 using customer table.
NAME column has fixed width, so the value (customer name) needs to be separated into more than one representation.
Please see:
NAME index="1"....
NAME index="2"....
Any idea how to tackle this?
Thank you,
Anne
<PARTNER>
<NAME index="1">XEXSY SMALL REALTY LLC</NAME>
<NAME index="2">AA/NAX TEEEENERGY</NAME>
<PARTNRTYPE>703884</PARTNRTYPE>
<ADDRESS>
<ADDRLINE index="1">544 PACIFIC BLVD</ADDRLINE>
<CITY>LONG BEACH</CITY>
<COUNTRY>US</COUNTRY>
<POSTALCODE>07740</POSTALCODE>
</ADDRESS>
</PARTNER>
This design is awfull. If you have to the slightest chance to change this, you should...
If you have to stick with this, you can try it like this:
DECLARE #mockup TABLE(Name VARCHAR(100),PartnerType INT,Addr VARCHAR(100),City VARCHAR(100));
INSERT INTO #mockup VALUES
('This is a very long name which needs to be splitted in smaller parts'
,12345
,'And this address is very long too, the person has a really long address...'
,'Washington')
,('ShortName'
,12345
,'ShortAddress'
,'New York');
--You can set the length of the slices. The TOP(20) is hardcoded and sets an upper limit to the count of parts.
DECLARE #PartLenght INT=20;
--The query will get a number's table (tally table) on-the-fly and then use FOR XML PATH() to create the XML with the nestings you need.
WITH Tally AS
(
SELECT TOP(20) ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS Nr
FROM master..spt_values
)
SELECT (
SELECT Nr AS [NAME/#index]
,SUBSTRING(m.Name,Nr+((Nr-1) * (#PartLenght-1)),#PartLenght) AS [NAME]
FROM Tally
WHERE LEN(SUBSTRING(m.Name,Nr+((Nr-1) * (#PartLenght-1)),#PartLenght))>0
FOR XML PATH(''),TYPE
)
,m.PartnerType AS [PARTNERTYPE]
,(
SELECT
(
SELECT Nr AS [ADDRLINE/#index]
,SUBSTRING(m.Addr,Nr+((Nr-1) * (#PartLenght-1)),#PartLenght) AS [ADDRLINE]
FROM Tally
WHERE LEN(SUBSTRING(m.Addr,Nr+((Nr-1) * (#PartLenght-1)),#PartLenght))>0
FOR XML PATH(''),TYPE
)
,City AS [CITY]
FOR XML PATH('ADDRESS'),TYPE
)
FROM #mockup AS m
FOR XML PATH('PARTNER')
The result
<PARTNER>
<NAME index="1">This is a very long </NAME>
<NAME index="2">name which needs to </NAME>
<NAME index="3">be splitted in small</NAME>
<NAME index="4">er parts</NAME>
<PARTNERTYPE>12345</PARTNERTYPE>
<ADDRESS>
<ADDRLINE index="1">And this address is </ADDRLINE>
<ADDRLINE index="2">very long too, the p</ADDRLINE>
<ADDRLINE index="3">erson has a really l</ADDRLINE>
<ADDRLINE index="4">ong address...</ADDRLINE>
<CITY>Washington</CITY>
</ADDRESS>
</PARTNER>
<PARTNER>
<NAME index="1">ShortName</NAME>
<PARTNERTYPE>12345</PARTNERTYPE>
<ADDRESS>
<ADDRLINE index="1">ShortAddress</ADDRLINE>
<CITY>New York</CITY>
</ADDRESS>
</PARTNER>

How to convert nested XML into corresponding tables?

I have a complex nested XML (generated from a C# entity graph), for example:
<Customers>
<Customer>
<Id>1</Id>
<Number>12345</Number>
<Addresses>
<Address>
<Id>100</Id>
<Street>my street </street>
<city>London</city>
</Address>
<Address>
<Id>101</Id>
<street>my street 2</street>
<city>Berlin</city>
</Address>
</Addresses>
<BankDetails>
<BankDetail>
<Id>222</Id>
<Iban>DE8439834934939434333</Iban>
</BankDetail>
<BankDetail>
<Id>228</Id>
<Iban>UK1237921391239123213</Iban>
</BankDetail>
</BankDetails>
<Orders>
<Order>
<OrderLine>
</OrderLine>
</Order>
</Orders>
</Customer>
</Customers>
Before saving the above XML data into the actual tables, I need to process it first. For this reason, I created corresponding table types. Each of these table types have an extra column (guid as ROWGUID) so that if I'm processing new data (not yet assigned primary key) I generate a unique key. I use this column to keep the relational integrity between different table types.
What is the SQL syntax to convert the above nested XML to their corresponding tables, keeping in mind that child records must reference the generated parent guid?
Try it like this:
DECLARE #xml XML=
N'<Customers>
<Customer>
<Id>1</Id>
<AccountNumber>12345</AccountNumber>
<Addresses>
<Address>
<Id>100</Id>
<street>my street></street>
<city>London</city>
</Address>
<Address>
<Id>101</Id>
<street>my street></street>
<city>Berlin</city>
</Address>
</Addresses>
<BankDetails>
<BankDetail>
<Id>222</Id>
<Iban>DE8439834934939434333</Iban>
</BankDetail>
<BankDetail>
<Id>228</Id>
<Iban>UK1237921391239123213</Iban>
</BankDetail>
</BankDetails>
<Orders>
<Order>
<OrderLine />
</Order>
</Orders>
</Customer>
</Customers>';
--This query will create a table #tmpInsert with all the data
SELECT cust.value('Id[1]','int') AS CustomerID
,cust.value('AccountNumber[1]','int') AS CustomerAccountNumber
,addr.value('Id[1]','int') AS AddressId
,addr.value('street[1]','nvarchar(max)') AS AddressStreet
,addr.value('city[1]','nvarchar(max)') AS AddressCity
,bank.value('Id[1]','int') AS BankId
,bank.value('Iban[1]','nvarchar(max)') AS BankIban
,ord.value('OrderLine[1]','nvarchar(max)') AS OrderLine
INTO #tmpInsert
FROM #xml.nodes('/Customers/Customer') AS A(cust)
OUTER APPLY cust.nodes('Addresses/Address') AS B(addr)
OUTER APPLY cust.nodes('BankDetails/BankDetail') AS C(bank)
OUTER APPLY cust.nodes('Orders/Order') AS D(ord);
--Here you can check the content
SELECT * FROM #tmpInsert;
--Clean-Up
GO
DROP TABLE #tmpInsert
Once you've got all your data in the table, you can use simple DISTINCT, GROUP BY, if needed ROW_NUMBER() OVER(PARTITION BY ...) to select each set separately for the proper insert.

Given the following XML in SQL Server, how do I get a value?

Here is my SQL. I cannot seem to get one single value out of this thing. It only works if I remove all of the xmlns attributes.
I think the problem is that this xml contains 2 default namespaces, one attached to the Response element and one attached to the Shipment element.
DECLARE #xml XML
SET #xml = '<TrackResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Response xmlns="http://www.ups.com/XMLSchema/XOLTWS/Common/v1.0">
<ResponseStatus>
<Code>1</Code>
<Description>Success</Description>
</ResponseStatus>
<TransactionReference />
</Response>
<Shipment xmlns="http://www.ups.com/XMLSchema/XOLTWS/Track/v2.0">
<InquiryNumber>
<Code>01</Code>
<Description>ShipmentIdentificationNumber</Description>
<Value>1ZA50209234098230</Value>
</InquiryNumber>
<ShipperNumber>A332098</ShipperNumber>
<ShipmentAddress>
<Type>
<Code>01</Code>
<Description>Shipper Address</Description>
</Type>
<Address>
<AddressLine>123 HWY X</AddressLine>
<City>SOMETOWN</City>
<StateProvinceCode>SW</StateProvinceCode>
<PostalCode>20291 1234</PostalCode>
<CountryCode>US</CountryCode>
</Address>
</ShipmentAddress>
<ShipmentWeight>
<UnitOfMeasurement>
<Code>LBS</Code>
</UnitOfMeasurement>
<Weight>0.00</Weight>
</ShipmentWeight>
<Service>
<Code>42</Code>
<Description>UPS GROUND</Description>
</Service>
<Package>
<TrackingNumber>1ZA50209234098230</TrackingNumber>
<PackageServiceOption>
<Type>
<Code>01</Code>
<Description>Signature Required</Description>
</Type>
</PackageServiceOption>
<Activity>
<ActivityLocation>
<Address>
<City>SOMEWHERE</City>
<StateProvinceCode>PA</StateProvinceCode>
<CountryCode>US</CountryCode>
</Address>
</ActivityLocation>
<Status>
<Type>X</Type>
<Description>Damage reported. / Damage claim under investigation.</Description>
<Code>UY</Code>
</Status>
<Date>20120424</Date>
<Time>125000</Time>
</Activity>
<Activity>
<ActivityLocation>
<Address>
<City>SOMEWHERE</City>
<StateProvinceCode>PA</StateProvinceCode>
<CountryCode>US</CountryCode>
</Address>
</ActivityLocation>
<Status>
<Type>X</Type>
<Description>All merchandise discarded. UPS will notify the sender with details of the damage.</Description>
<Code>GY</Code>
</Status>
<Date>20120423</Date>
<Time>115500</Time>
</Activity>
<PackageWeight>
<UnitOfMeasurement>
<Code>LBS</Code>
</UnitOfMeasurement>
<Weight>0.00</Weight>
</PackageWeight>
</Package>
</Shipment>
</TrackResponse>'
select Svc.Dsc.value('(/TrackResponse/Shipment/Service/Description)[1]', 'varchar(25)')
from #xml.nodes('/TrackResponse') as Svc(Dsc)
As #marc_s said, you are ignoring xml namespaces. Here is a sql fiddle example. This gives X, I think that is what you need. Read this article for more. Note : *:TrackResponse[1]/*: in the xpath
--Results: X
declare #xmlTable as table (
xmlData xml
)
insert into #xmlTable
select '<TrackResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Response xmlns="http://www.ups.com/XMLSchema/XOLTWS/Common/v1.0">
...
</TrackResponse>'
;with xmlnamespaces(default 'http://www.ups.com/XMLSchema/XOLTWS/Track/v2.0')
select
x.xmlData.value('(/*:TrackResponse[1]/*:Shipment[1]/Package[1]/Activity[1]/Status[1]/Type[1])','varchar(100)') as all_snacks
from #xmlTable x
Two problems:
you're blatantly ignoring the XML namespace that's defined on the <shipment> element
your XQuery expression was a bit off
Try this:
-- define XML namespace
;WITH XMLNAMESPACES('http://www.ups.com/XMLSchema/XOLTWS/Track/v2.0' AS ns)
select
Svc.Dsc.value('(ns:Shipment/ns:Service/ns:Description)[1]', 'varchar(25)')
from
-- this already selects all <TrackResponse> nodes - no need to repeat that in
-- your above call to .value()
#xml.nodes('/TrackResponse') as Svc(Dsc)
Gives me a result of:
UPS GROUND