0

Sorry,

Been struggle with this query for a while

declare @a xml
select @a='<GrsAutoCompleteCodes>
  <GrsAutoCompleteCode CitiCode="00DX" IsMatched="0" HasCustomName="0" />
  <GrsAutoCompleteCode CitiCode="00G0" IsMatched="0" HasCustomName="0" />
</GrsAutoCompleteCodes>'

SELECT       
 p.s.value('(CitiCode/text())[1]','VARCHAR(100)') AS CitiCode
FROM @a.nodes('/GrsAutoCompleteCode') p(s) 

This query somehow returns no records, what am I doing wrongly?

1 Answer 1

1

Because it's completely wrong, I'm afraid. Your root is GrsAutoCompleteCodes not GrsAutoCompleteCode, and CitiCode isn't a node it's a property of GrsAutoCompleteCode.

Perhaps you want this, which returns the value of CitiCode for the first GrsAutoCompleteCode node:

DECLARE @a xml;
SET @a = '<GrsAutoCompleteCodes>
  <GrsAutoCompleteCode CitiCode="00DX" IsMatched="0" HasCustomName="0" />
  <GrsAutoCompleteCode CitiCode="00G0" IsMatched="0" HasCustomName="0" />
</GrsAutoCompleteCodes>'

SELECT a.GACC.value('(GrsAutoCompleteCode/@CitiCode)[1]','varchar(4)') AS CitiCode
FROM @a.nodes('/GrsAutoCompleteCodes')a(GACC);

If you want the value of every CitiCode, that would be this:

SELECT a.GACC.value('@CitiCode','varchar(4)') AS CitiCode
FROM @a.nodes('/GrsAutoCompleteCodes/GrsAutoCompleteCode')a(GACC);
Sign up to request clarification or add additional context in comments.

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.