0

I have a log table with XML column. I studied several examples of querying XML but they don't match my needs.

Id   TimeStamp            Message    Properties
------------------------------------------------------------------------------
1    2015-10-27 08:45:38  Text       <properties><property key="time">23.10.2015 15:00:40</property><property key="interceptors">False</property></properties>
2    2015-10-27 09:21:09  Text       <properties><property key="Product">Azure</property><property key="Source">Azure Infrastructure</property></properties>
3    2015-10-27 16:51:32  Text       <properties><property key="Product">Azure</property><property key="Source">Azure Infrastructure</property><property key="Uptime">00:00:27.3874982</property></properties>
4    2015-10-28 11:03:25  Text       <properties><property key="Uptime">00:19:21.4729461</property></properties>

I want to have a result set of all node values where the property node has a key of 'uptime'. The result set could look like:

Id   Uptime
-----------------
3    00:00:27.3874982
4    00:19:21.4729461

2 Answers 2

1

Below is one method which uses xquery expression with an attribute filter along with a CTE to avoid repeating the expression:

WITH Uptimes AS (
    SELECT
          Id
        , Properties.value('(/properties/property[@key="Uptime"])[1]', 'time(7)') AS Uptime
    FROM dbo.test
    )
SELECT Id, Uptime
FROM Uptimes
WHERE Uptime IS NOT NULL;
Sign up to request clarification or add additional context in comments.

Comments

1

You can use below sql

select id, act.value('(/properties/property[@key="Uptime"])[1]', 'varchar(25)')
from #t
where act.exist('/properties/property[@key="Uptime"]') = 1

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.