0

I want to sum xml node values, but luck is not favouring, please help.

select 
    animals_quantity.value('data(/AnimalsAddData/Quantity/@value)','int') 
from 
    tbl_animals
1
  • 2
    Show us a sample of the XML ! Commented Aug 12, 2016 at 12:54

2 Answers 2

1

Take values from node and pass them to SUM aggregate:

DECLARE @xml xml = '
<AnimalsAddData>
    <Quantity value="4" />
    <Quantity value="1" />
    <Quantity value="10" />
    <Quantity value="200" />
</AnimalsAddData>'

SELECT SUM(N.value('.', 'int'))
FROM @xml.nodes('/AnimalsAddData/Quantity/@value') T(N)

Make sure you include sample data.

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

Comments

0

I find a very easy solution for it.

SELECT  sum(CAST(CAST(CAST(animals_quantity AS XML) AS VARCHAR(100)) AS INT)) as total from tbl_animals

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.