HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

XML Query Question

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
queryquestionxml

Problem

I'm trying to generate a SQL query to get the "DATE" values out of the following SQL XML:

I've tried something like this but I don't think I'm understanding the concepts.

select 
   xConfig.value('(/SearchjobConfig/QueryString/SearchCriteria/ExpressionSet/SimpleAttributeExpression) [1]','nvarchar(max)') 
from 
    Job


Here is the XML as text:


1072



2019-06-01T04:00:00
2019-06-13T03:59:59
65






The expected output would be the Dates:

2019-06-01T04:00:00
2019-06-13T03:59:59


and what would it take to get the results on the same line.. example:

date_val_start date_val_end 2019-06-01T04:00:00 2019-06-13T03:59:59


I'm using SQL Server 2012 Enterprise Edition.

Solution

Does this get you what you want?

DECLARE @x XML = '

  1072
  
    
      
        2019-06-01T04:00:00
        2019-06-13T03:59:59
        65
      
    
  

'

DECLARE @Job TABLE(xConfig XML)

INSERT @Job ( xConfig )
VALUES ( @x )

SELECT j.*
       , ca.c.value('text()[1]', 'VARCHAR(30)') AS date_val
FROM @Job AS j
CROSS APPLY j.xConfig.nodes('/SearchJobConfig/QueryString/SearchCriteria/ExpressionSet/SimpleAttributeExpression') AS ca(c)
WHERE ca.c.exist('@displayName[.= "Date"]') = 1;


To get everything on one line, just do this:

SELECT *
FROM 
(
    SELECT STUFF(
    (SELECT N' ' + ca.c.value('text()[1]', 'NVARCHAR(MAX)')
     FROM @Job AS j
     CROSS APPLY j.xConfig.nodes('/SearchJobConfig/QueryString/SearchCriteria/ExpressionSet/SimpleAttributeExpression') AS ca(c)
     WHERE ca.c.exist('@displayName[.= "Date"]') = 1
     FOR XML PATH(N''), TYPE ).value(N'.[1]', N'NVARCHAR(MAX)'), 1, 1, N'') 
) AS x(date_val);

Code Snippets

DECLARE @x XML = '
<SearchJobConfig>
  <QueryID>1072</QueryID>
  <QueryString>
    <SearchCriteria name="Search query" >
      <ExpressionSet logicalOperator="AND">
        <SimpleAttributeExpression displayName="Date" npmPropertyId="4" searchOperation="GREATER_EQUAL" dataType="string" caseSensitive="false">2019-06-01T04:00:00</SimpleAttributeExpression>
        <SimpleAttributeExpression displayName="Date" npmPropertyId="4" searchOperation="LESS_EQUAL" dataType="string" caseSensitive="false">2019-06-13T03:59:59</SimpleAttributeExpression>
        <SimpleAttributeExpression displayName="Class" npmPropertyId="1056" searchOperation="EQUALS" dataType="int32" caseSensitive="false">65</SimpleAttributeExpression>
      </ExpressionSet>
    </SearchCriteria>
  </QueryString>
</SearchJobConfig>
'

DECLARE @Job TABLE(xConfig XML)

INSERT @Job ( xConfig )
VALUES ( @x )

SELECT j.*
       , ca.c.value('text()[1]', 'VARCHAR(30)') AS date_val
FROM @Job AS j
CROSS APPLY j.xConfig.nodes('/SearchJobConfig/QueryString/SearchCriteria/ExpressionSet/SimpleAttributeExpression') AS ca(c)
WHERE ca.c.exist('@displayName[.= "Date"]') = 1;
SELECT *
FROM 
(
    SELECT STUFF(
    (SELECT N' ' + ca.c.value('text()[1]', 'NVARCHAR(MAX)')
     FROM @Job AS j
     CROSS APPLY j.xConfig.nodes('/SearchJobConfig/QueryString/SearchCriteria/ExpressionSet/SimpleAttributeExpression') AS ca(c)
     WHERE ca.c.exist('@displayName[.= "Date"]') = 1
     FOR XML PATH(N''), TYPE ).value(N'.[1]', N'NVARCHAR(MAX)'), 1, 1, N'') 
) AS x(date_val);

Context

StackExchange Database Administrators Q#240586, answer score: 8

Revisions (0)

No revisions yet.