XML Query QuestionSQL Server stored procedure working when run manually, not running from SQL Server AgentParsing Typed XML with a default namespace and inconsistent root nodeAlternative for WHERE clause in SQL queryHow to safely recreate a MS SQL table that has other objects depending on it?Update XML metadata in SQL ServerIssue installing SQL Server 2012 SP4 in a cluster - missing ‘vc_red.msi’INSERT/SELECT xml column from one table to anotherFind max content length of column of particular table from entire SQL serverT-SQL LIKE Predicate failed to match with whitespace in XML converted varcharCount rows in a table based on a DATE field
How are side-channel attacks executed? What does an attacker need to execute a side channel attack?
/etc/shadow permissions security best practice (000 vs. 600 vs. 640)
How time is defined in astronomical science
Is Basalt Monolith a 1-card infinite combo with no payoff?
Can Alter Self be used to enter an enemy's body and destroy it from the inside?
Does the Presidential Records Act apply to the Trump/Zelensky telephone call 25 July 2019?
Graph with cropped letters
Slash the matrix
Decrypt T-SQL log backup header and read LSN
Wood versus marble rolling pin 'performance'
Why the real and imaginary parts of a complex analytic function are not independent?
Would webs catch fire if Fire Bolt is used on a creature inside of them?
Teaching a student who refuses to learn
Is it possible to save a (science) PhD in 10 months?
I was mistakenly identified as a criminal, and this has caused rumors. How can I convince my friends that it is all a mistake?
How to control the padding below a BarLegend?
What clothing should I bring to Mt. Titlis in Switzerland during December?
Does the original Game Boy game "Tetris" have a battery memory inside the cartridge?
How exactly do you avoid fooling yourself?
There are polygons with only right angles which have an odd number of corners
How to persuade players not to cheat?
Is the "Watchmen" TV series a continuation of the movie or the comics?
Has an engineer called Trevor Jackson invented a revolutionary battery allowing for a car range of 1500 miles?
how can I enforce the prohibition on love potions?
XML Query Question
SQL Server stored procedure working when run manually, not running from SQL Server AgentParsing Typed XML with a default namespace and inconsistent root nodeAlternative for WHERE clause in SQL queryHow to safely recreate a MS SQL table that has other objects depending on it?Update XML metadata in SQL ServerIssue installing SQL Server 2012 SP4 in a cluster - missing ‘vc_red.msi’INSERT/SELECT xml column from one table to anotherFind max content length of column of particular table from entire SQL serverT-SQL LIKE Predicate failed to match with whitespace in XML converted varcharCount rows in a table based on a DATE field
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
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:
<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>
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.
sql-server sql-server-2012 query xml
add a comment
|
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:
<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>
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.
sql-server sql-server-2012 query xml
add a comment
|
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:
<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>
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.
sql-server sql-server-2012 query xml
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:
<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>
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.
sql-server sql-server-2012 query xml
sql-server sql-server-2012 query xml
edited Jun 15 at 14:31
Paul White♦
55.7k16 gold badges311 silver badges491 bronze badges
55.7k16 gold badges311 silver badges491 bronze badges
asked Jun 14 at 14:45
JohnJohn
412 bronze badges
412 bronze badges
add a comment
|
add a comment
|
3 Answers
3
active
oldest
votes
Does this get you what you want?
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;
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);
add a comment
|
If you want the date values in the same row but different columns you can shred using nodes()
to get one row per ExpressionSet
node and then use attributes displayName
and searchOption
in value()
to get start and end date.
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 T.X.value('(SimpleAttributeExpression
[
@displayName = "Date" and
@searchOperation = "GREATER_EQUAL"
]/text())[1]', 'datetime') as date_val_start,
T.X.value('(SimpleAttributeExpression
[
@displayName = "Date" and
@searchOperation = "LESS_EQUAL"
]/text())[1]', 'datetime') as date_val_end
from @Job as j
cross apply j.xConfig.nodes('/SearchJobConfig/QueryString/
SearchCriteria/ExpressionSet') as T(X);
Result:
date_val_start date_val_end
2019-06-01 04:00:00.000 2019-06-13 03:59:59.000
add a comment
|
You say that you want to retrieve multiple values from the XML, but then instructing your XPath to return only the first (#1) of them:
/ SearchjobConfig / QueryString / SearchCriteria / ExpressionSet / SimpleAttributeExpression [ 1 ]
___/
Returns indexed item #1 _/
Lose this indexing clause and you'll get both values back, in the order they appear within the input XML.
Further reading: https://www.w3schools.com/xml/xpath_syntax.asp
As far as getting them into a single line ... it's [almost certainly] possibly, but is it really worth the effort for only two of them? YMMV.
add a comment
|
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "182"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f240586%2fxml-query-question%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Does this get you what you want?
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;
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);
add a comment
|
Does this get you what you want?
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;
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);
add a comment
|
Does this get you what you want?
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;
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);
Does this get you what you want?
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;
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);
edited Jun 14 at 20:52
answered Jun 14 at 19:15
Erik DarlingErik Darling
28.1k13 gold badges86 silver badges144 bronze badges
28.1k13 gold badges86 silver badges144 bronze badges
add a comment
|
add a comment
|
If you want the date values in the same row but different columns you can shred using nodes()
to get one row per ExpressionSet
node and then use attributes displayName
and searchOption
in value()
to get start and end date.
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 T.X.value('(SimpleAttributeExpression
[
@displayName = "Date" and
@searchOperation = "GREATER_EQUAL"
]/text())[1]', 'datetime') as date_val_start,
T.X.value('(SimpleAttributeExpression
[
@displayName = "Date" and
@searchOperation = "LESS_EQUAL"
]/text())[1]', 'datetime') as date_val_end
from @Job as j
cross apply j.xConfig.nodes('/SearchJobConfig/QueryString/
SearchCriteria/ExpressionSet') as T(X);
Result:
date_val_start date_val_end
2019-06-01 04:00:00.000 2019-06-13 03:59:59.000
add a comment
|
If you want the date values in the same row but different columns you can shred using nodes()
to get one row per ExpressionSet
node and then use attributes displayName
and searchOption
in value()
to get start and end date.
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 T.X.value('(SimpleAttributeExpression
[
@displayName = "Date" and
@searchOperation = "GREATER_EQUAL"
]/text())[1]', 'datetime') as date_val_start,
T.X.value('(SimpleAttributeExpression
[
@displayName = "Date" and
@searchOperation = "LESS_EQUAL"
]/text())[1]', 'datetime') as date_val_end
from @Job as j
cross apply j.xConfig.nodes('/SearchJobConfig/QueryString/
SearchCriteria/ExpressionSet') as T(X);
Result:
date_val_start date_val_end
2019-06-01 04:00:00.000 2019-06-13 03:59:59.000
add a comment
|
If you want the date values in the same row but different columns you can shred using nodes()
to get one row per ExpressionSet
node and then use attributes displayName
and searchOption
in value()
to get start and end date.
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 T.X.value('(SimpleAttributeExpression
[
@displayName = "Date" and
@searchOperation = "GREATER_EQUAL"
]/text())[1]', 'datetime') as date_val_start,
T.X.value('(SimpleAttributeExpression
[
@displayName = "Date" and
@searchOperation = "LESS_EQUAL"
]/text())[1]', 'datetime') as date_val_end
from @Job as j
cross apply j.xConfig.nodes('/SearchJobConfig/QueryString/
SearchCriteria/ExpressionSet') as T(X);
Result:
date_val_start date_val_end
2019-06-01 04:00:00.000 2019-06-13 03:59:59.000
If you want the date values in the same row but different columns you can shred using nodes()
to get one row per ExpressionSet
node and then use attributes displayName
and searchOption
in value()
to get start and end date.
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 T.X.value('(SimpleAttributeExpression
[
@displayName = "Date" and
@searchOperation = "GREATER_EQUAL"
]/text())[1]', 'datetime') as date_val_start,
T.X.value('(SimpleAttributeExpression
[
@displayName = "Date" and
@searchOperation = "LESS_EQUAL"
]/text())[1]', 'datetime') as date_val_end
from @Job as j
cross apply j.xConfig.nodes('/SearchJobConfig/QueryString/
SearchCriteria/ExpressionSet') as T(X);
Result:
date_val_start date_val_end
2019-06-01 04:00:00.000 2019-06-13 03:59:59.000
answered Jul 5 at 7:26
Mikael ErikssonMikael Eriksson
19.3k3 gold badges49 silver badges90 bronze badges
19.3k3 gold badges49 silver badges90 bronze badges
add a comment
|
add a comment
|
You say that you want to retrieve multiple values from the XML, but then instructing your XPath to return only the first (#1) of them:
/ SearchjobConfig / QueryString / SearchCriteria / ExpressionSet / SimpleAttributeExpression [ 1 ]
___/
Returns indexed item #1 _/
Lose this indexing clause and you'll get both values back, in the order they appear within the input XML.
Further reading: https://www.w3schools.com/xml/xpath_syntax.asp
As far as getting them into a single line ... it's [almost certainly] possibly, but is it really worth the effort for only two of them? YMMV.
add a comment
|
You say that you want to retrieve multiple values from the XML, but then instructing your XPath to return only the first (#1) of them:
/ SearchjobConfig / QueryString / SearchCriteria / ExpressionSet / SimpleAttributeExpression [ 1 ]
___/
Returns indexed item #1 _/
Lose this indexing clause and you'll get both values back, in the order they appear within the input XML.
Further reading: https://www.w3schools.com/xml/xpath_syntax.asp
As far as getting them into a single line ... it's [almost certainly] possibly, but is it really worth the effort for only two of them? YMMV.
add a comment
|
You say that you want to retrieve multiple values from the XML, but then instructing your XPath to return only the first (#1) of them:
/ SearchjobConfig / QueryString / SearchCriteria / ExpressionSet / SimpleAttributeExpression [ 1 ]
___/
Returns indexed item #1 _/
Lose this indexing clause and you'll get both values back, in the order they appear within the input XML.
Further reading: https://www.w3schools.com/xml/xpath_syntax.asp
As far as getting them into a single line ... it's [almost certainly] possibly, but is it really worth the effort for only two of them? YMMV.
You say that you want to retrieve multiple values from the XML, but then instructing your XPath to return only the first (#1) of them:
/ SearchjobConfig / QueryString / SearchCriteria / ExpressionSet / SimpleAttributeExpression [ 1 ]
___/
Returns indexed item #1 _/
Lose this indexing clause and you'll get both values back, in the order they appear within the input XML.
Further reading: https://www.w3schools.com/xml/xpath_syntax.asp
As far as getting them into a single line ... it's [almost certainly] possibly, but is it really worth the effort for only two of them? YMMV.
answered Jul 5 at 12:11
Phill W.Phill W.
1,6735 silver badges6 bronze badges
1,6735 silver badges6 bronze badges
add a comment
|
add a comment
|
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f240586%2fxml-query-question%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown