1

This query returns the attribute value "storedId1"

SET @xml = N'<Data>
                <Ref ID="1" sf="storedId1">
                this is the value I want
                </Ref>
            </Data>'

SELECT 
    T.C.value('@sf', 'nvarchar(MAX)') AS result
FROM  
    @xml.nodes('Data/Ref') T(C)
WHERE
    T.C.value('@sf', 'nvarchar(MAX)') = 'storedId1'

How can I return the node value instead .i.e. return "this is the value i want"

I guess I need to change the SELECT to something like

SELECT T.C.value('Data/Ref', 'nvarchar(MAX)') AS result

But it fails with

XQuery [value()]: 'value()' requires a singleton (or empty sequence), found operand of type 'xdt:untypedAtomic *'

2 Answers 2

3

Try this:

SELECT 
    T.C.value('(./text())[1]', 'nvarchar(MAX)') AS result
FROM  
    @xml.nodes('Data/Ref[@sf="storedId1"]') T(C)
Sign up to request clarification or add additional context in comments.

Comments

2
DECLARE @xml xml   
SET @xml = N'<Data>
                <Ref ID="1" sf="storedId1">
                this is the value I want
                </Ref>
            </Data>'

SELECT 
    T.C.value('.', 'nvarchar(MAX)') AS result
FROM  
    @xml.nodes('Data/Ref') T(C)
WHERE
    T.C.value('@sf', 'nvarchar(MAX)') = 'storedId1'

Demo: http://sqlfiddle.com/#!18/a7540/28512

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.