Results 1 to 4 of 4

Thread: SQL Server 2k5 - XML query

  1. #1

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    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

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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.

  3. #3

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    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

  4. #4
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    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
  •  



Click Here to Expand Forum to Full Width