|
-
Nov 12th, 2008, 01:56 PM
#1
SQL Server 2k5 - XML query
I don't know if this is directly an XML oriented question, or if SQL Server has something to do with it, but here it goes.
I've got my select statement
SELECT Data.query('sum(//Needs/CurrentIncome)') FROM Cache
Where Data is an xml column, and the CurrentIncome nodes of the xml blob all contain numeric data. The problem is that the result of the sum() call is returning the numbers in scientific notation. Which means I can't convert the results to numbers, which is what I really need to do. The results are large numbers, but no where near any sort of numeric data type limit.
So is there something I can do to change the result format?
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Nov 12th, 2008, 02:47 PM
#2
Re: SQL Server 2k5 - XML query
Try CAST AS REAL or as a DECIMAL with a large precision (like DECIMAL(50,5)) so that it's 50 places before the decimal and 2 after. I believe you can do the same with NUMERIC(x,y) but it'll give you trailing zeros which you could trim if you need to.
-
Nov 12th, 2008, 02:55 PM
#3
Re: SQL Server 2k5 - XML query
SELECT CAST(Data.query('sum(//Needs/CurrentIncome)') AS DECIMAL(38,5)) FROM Cache
Gets me : Explicit conversion from data type xml to decimal is not allowed.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Nov 12th, 2008, 03:29 PM
#4
Re: SQL Server 2k5 - XML query
You could create a user defined function that accepts an xml parameter, and use something like this:
Code:
declare @data xml; set @data = '
<Data>
<Needs>
<CurrentIncome>500000</CurrentIncome>
</Needs>
<Needs>
<CurrentIncome>750000</CurrentIncome>
</Needs>
</Data>'
SELECT sum(INCOME.value('.','money'))
From @Data.nodes('//Needs/CurrentIncome') as Data(INCOME)
That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma
Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|