Choose a XML node in SQL Server based on max value of a child element
Posted
by Jay
on Stack Overflow
See other posts from Stack Overflow
or by Jay
Published on 2010-06-17T13:01:14Z
Indexed on
2010/06/17
13:03 UTC
Read the original article
Hit count: 286
I am trying to select from SQL Server 2005 XML datatype some values based on the max data that is located in a child node.
I have multiple rows with XML similar to the following stored in a field in SQL Server:
<user>
<name>Joe</name>
<token>
<id>ABC123</id>
<endDate>2013-06-16 18:48:50.111</endDate>
</token>
<token>
<id>XYX456</id>
<endDate>2014-01-01 18:48:50.111</endDate>
</token>
</user>
I want to perform a select from this XML column where it determines the max date within the token element and would return the datarows similar to the result below for each record:
Joe XYZ456 2014-01-01 18:48:50.111
I have tried to find a max function for xpath that would all me to select the correct token element but I couldn't find one that would work.
I also tried to use the SQL MAX function but I wasn't able to get it working with that method either.
If I only have a single token it of course works fine but when I have more than one I get a NULL, most likely because the query doesn't know which date to pull. I was hoping there would be a way to specify a where clause [max(endDate)]
on the token element but haven't found a way to do that.
Here is an example of the one that works when I only have a single token:
SELECT
XMLCOL.query('user/name').value('.','NVARCHAR(20)') as name
XMLCOL.query('user/token/id').value('.','NVARCHAR(20)') as id
XMLCOL.query('user/token/endDate').value(,'xs:datetime(.)','DATETIME') as endDate
FROM MYTABLE
© Stack Overflow or respective owner