Insomniac
Insomniac

Reputation: 3384

SQL Server: Order by XML column's node

I was wondering can I ORDER BY my query by node of an XML typed column?

For example I have a table

ID (int) | Data (XML)

Where Data column stores XML in form similar to this

<?xml?>
<Data>
   <SimpleOrderedValue>1</SimpleOrderedValue>
   <ComplicatedInternals>
      ...
   </ComplicatedInternals>
</Data>

I want query this table ordered by SimpleOrderedValue. Can I do this on MS SQL Server 2008 with XML column? Or can I do thins using CLR UDT, but without additional computed column and in a way so that Data column were indexed (for faster search).

Will appreciate any help. Thanks.

Upvotes: 3

Views: 6766

Answers (1)

Alex K.
Alex K.

Reputation: 175768

How about an XQuery expression;

select id, data
from T
  order by data.value('(/Data/SimpleOrderedValue)[1]', 'int') 

Upvotes: 4

Related Questions