2

I would like select the value of Type where the xml below is in an xml typed column in a table in a SQL Server 2005 database.

<Something xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Id xmlns="http://something.com/">1921</Id>
  <Date xmlns="http://something.com/">1901-01-01T00:00:00Z</Date>
  <Schedule xmlns="http://something.com/">Region</Schedule>
  <Filters xmlns="http://something.com/">
    <Data>
      <Id>99999</Id>
      <Name>CS</Name>
      <Type>SomeType</Type>
    </Data>
  </Filters>
</Something>

1 Answer 1

2

Try something like this:

;WITH XMLNAMESPACES ('http://something.com/' AS xn)
SELECT
    XmlType = XmlColumn.value('(Something/xn:Filters/xn:Data/xn:Type)[1]', 'varchar(50)') 
FROM 
    dbo.YourTable

Basically, your root element <Something> isn't part of any explicit XML namespace - but any of the elements under <Filters> are - so therefore, you need to qualify those with an appropriate XML namespace prefix to get at the data inside the XML element.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.