14,494 questions
Hi @Naomi Nosonovsky,
The XQuery .exist()
method to the rescue.
Notable points:
- We are using a wildcard XML namespace in the XPath expression because we don't know your true XML structure and its namespaces.
- XPath predicate
[text()]
will retrieve rows where D_STC12_FreeFormMessageText XML element present and has values, i.e. rows where XML element has blank value won't be retrieved.
-- DDL and sample data population, start
DECLARE @tbl TABLE (id INT IDENTITY PRIMARY KEY, xmldata XML);
INSERT INTO @tbl (xmldata) VALUES
(N'<Interchanges>
<S_ISA xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="www.edifabric.com/x12">
<D_744_1>00</D_744_1>
<D_745_2/>
</S_ISA>
<S_GS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="www.edifabric.com/x12">
<D_STC12_FreeFormMessageText>H MISSING OR INVALID INFORMATION. ADDITIONAL DETAILS WILL FOLLOW IF AVAILABLE</D_STC12_FreeFormMessageText>
</S_GS>
</Interchanges>'),
(N'<Interchanges>
<S_ISA xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="www.edifabric.com/x12">
<D_744_1>00</D_744_1>
<D_745_2/>
</S_ISA>
<S_GS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="www.edifabric.com/x12">
</S_GS>
</Interchanges>');
-- DDL and sample data population, end
SELECT *
FROM @tbl
WHERE xmldata.exist('//*:D_STC12_FreeFormMessageText[text()]') = 1;