I have a table with a nvarchar
column which stores XML. I'm trying to find all rows which has a specific node empty
Here is the structure of the table JOB
:
ID NVARCAHR(64)
NAME NVARCAHR(128)
ExtraInfo NVARCAHR(MAX)
Here is a sample of the content of the column ExtraInfo
, I'm looking for:
<ModifyMetadata>
<hostName t="ws">host2</hostName>
<Name t="ws">myname</Name>
<modifiedFields t="lt">
<ExtraInfoAdditionalField>
<FieldName t="ws">foo</FieldName>
<Values t="lws">
<s/>
</Values>
<FieldType t="ws">String</FieldType>
</ExtraInfoAdditionalField>
<ExtraInfoAdditionalField>
<FieldName t="ws">bar</FieldName>
<Values t="lws">
<s/>
</Values>
<FieldType t="ws">String</FieldType>
</ExtraInfoAdditionalField>
</modifiedFields>
</ModifyMetadata>
With this sample basically I'm looking for all rows with the node ModifyMetadata
and where the nodes ExtraInfoAdditionalField\Values
are empty (this is my understanding of what <s/>
means).
First I tried with a simple LIKE
in with this query:
SELECT *
FROM [JOB]
WHERE ExtraInfo LIKE '%\>\<\/s\>%' ESCAPE '\'
But this query also returns rows where the nodes ExtraInfoAdditionalField\Values
have some content.
The XQuery way: I switched to another method where I'm trying to CAST
the column to XML and then return only the row where the nodes ExtraInfoAdditionalField\Values
are empty.
But then I'm getting confused about the methodolgy to query this column with XQuery or any other available methodology.
I have tried to CAST
directly in the SELECT
SELECT CAST(ExtraInfo AS XML)
FROM [JOB]
WHERE ExtraInfo.value('XQUERYHERE') IS NULL
But SQL Server doesn't know what ExtraInfo. value
is.
I tried to CAST
it in a variable:
DECLARE @ExtraInfo XML
SET @ExtraInfo = CAST([MYDB].[dbo].[JOB].ExtraInfo AS XML)
SELECT ExtraInfo
FROM [JOB]
WHERE @ExtraInfo.value('XQUERYHERE') IS NULL
But SQL doesn't understand what is [MYDB].[dbo].[JOB].ExtraInfo
I have tried to CAST
directly in the SELECT
and rename it:
SELECT CAST(ExtraInfo AS XML) as myinfo
FROM [JOB]
WHERE myinfo.value('XQUERYHERE') IS NULL
But T-SQL doesn't know what myinfo
is.
So I'm clearly lacking of understanding in what way It possible to XML query a column which is stored in NVARCHAR
format
I hope this I this is clear enough, thanks in advance for any input