Building a string from an xml

Hello!

Can someone help me with some sql that will return to me some data from an xml object?

I have the following xml:
<root>
  <FromDate>2016-03-01</FromDate>
  <ToDate>2016-03-25</ToDate>
  <Variables>
    <Variable>
      <Name>pain</Name>
    </Variable>
    <Variable>
      <Name>unit</Name>
    </Variable>
  </Variables>
  <Filters>
    <Filter>
      <Name>diagnose</Name>
      <Values>
        <Value>1461</Value>
        <Value>1462</Value>
      </Values>
    </Filter>
  </Filters>
</root>

Open in new window

I would like to extract the text of the <Value> nodes under the <root> <Filters> <Filter><Values> where the text in <root> <Filters> <Filter><Name> is "diagnose".

The resuting nvarchar should be "1461, 1462"

I am used to building strings like this from tables using something like:
 select @CategoryAxis = @CategoryAxis + '<Category>' +cast( measurementdate as nvarchar(12) ) + '</Category> ' from MeasurementStatisticsPerUnit

Open in new window

but i just cannot get my head arround XML.
soozhCEOAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

YZlatCommented:
Where is this XML? Is it in the database object, in the file, etc?
soozhCEOAuthor Commented:
the xml is in a variable.

declare @d xml ;

set @d = '<root>
  <FromDate>2016-03-01</FromDate>
  <ToDate>2016-03-25</ToDate>
  <Variables>
    <Variable>
      <Name>pain</Name>
    </Variable>
    <Variable>
      <Name>unit</Name>
    </Variable>
  </Variables>
  <Filters>
    <Filter>
      <Name>diagnose</Name>
      <Values>
        <Value>1461</Value>
        <Value>1462</Value>
      </Values>
    </Filter>
  </Filters>
</root>' ;
YZlatCommented:
Is your xml variable inside of a stored procedure?
soozhCEOAuthor Commented:
yes this code is in a stored procedure
Arifhusen AnsariBusiness Intelligence Developer and AnalystCommented:
Try below code.


declare @xmlData as xml=
'<root>
  <FromDate>2016-03-01</FromDate>
  <ToDate>2016-03-25</ToDate>
  <Variables>
    <Variable>
      <Name>pain</Name>
    </Variable>
    <Variable>
      <Name>unit</Name>
    </Variable>
  </Variables>
  <Filters>
    <Filter>
      <Name>diagnose</Name>
      <Values>

        <Value>1461</Value>
        <Value>1462</Value>
      </Values>
    </Filter>
  </Filters>
</root>'


SELECT CASE WHEN CAST(@xmlData.query('/root/Filters/Filter/Name/text()') AS NVARCHAR(30))='diagnose' THEN 
REPLACE(REPLACE(REPLACE(CAST(@xmlData.query('/root/Filters/Filter/Values/Value') AS NVARCHAR(MAX)),'</Value><Value>',','),'<Value>',''),'</Value>','')
ELSE ''
END


----AS FormGuid

--SELECT REPLACE(REPLACE(REPLACE(CAST(@xmlData.query('/root/Filters/Filter/Values/Value') AS NVARCHAR(MAX)),'</Value><Value>',','),'<Value>',''),'</Value>','')

--SELECT @xmlData.query('/root/Filters/Filter/Name/text()/data/item[@name="form-data-guid"]/text()') AS FormGuid




--DECLARE @x xml
--SET @x = '<ROOT><a>111</a>
--<a>222</a> </ROOT>'
--SELECT @x.query('/ROOT/a')

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.