1

How to get the value of 'note' tag value with a carriage return?
Here is my code:

declare @Input xml='<Root>
  <Addresses>
    <Address>
      <note>AAA</note>
    </Address>
    <Address>
      <note>BBB</note>
    </Address>
    <Address>
      <note>CCC</note>
    </Address>
    <Address>
      <note>DDD</note>
    </Address>    
  </Addresses>
</Root>'

 SELECT CONVERT(nvarchar(max),m.c.query('.//Address/note/text()')) AS Comment   
 FROM @Input.nodes('Root/Addresses') AS m ( c )

I am expecting a result like below instead of 'AAABBBCCCDDD'

AAA
BBB
CCC
DDD
1
  • 1
    This question is pretty similar and should help, the only difference appears to be the other question wants a comma as the delimiter whereas you require a carriage return. Commented Jun 25, 2015 at 8:00

1 Answer 1

2

You can use something like this to get desired result:

declare @Result nvarchar(max)

select @Result = isnull(@Result + char(13) + char(10), '') + m.c.value('.', 'nvarchar(max)')
from @Input.nodes('Root/Addresses/Address/note') as m(c)

select @Result
Sign up to request clarification or add additional context in comments.

2 Comments

I was expecting the result as same as GarethD mentioned in the comment. Anyways thank you for your solution.
@Pradeep oh, I haven't figured out what you need to concatenate your values in one string having newline as delimiter. I've updated my answer with alternative approach - have a look, maybe it will fit your requirement.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.