For XML to decimal datatype

  • Hi,

    I'm trying to convert relational data to XML using For XML. I've managed to get the XML structure however columns stored as a float in the table are appearing as scientific in the XML. I have tried cast and convert but I can't get the output to 2 decimal places.

    If a value in column1 is 14.43.

    Select

    ISNULL(convert(float,[column1]),2) as [column1]

    for XML RAW('row'), elements, root('XMLData'), type

    gives 1.443000000000000e+001

    while

    Select

    ISNULL(convert(decimal,[column1]),2) as [column1]

    for XML RAW('row'), elements, root('XMLData'), type

    gives 14

  • floating point is exactly that. It's not a "precise" data type, meaning it won't truncate when you round a float.

    You're unsing the wrong data type if you wish exactly numbers of digits. Try Numeric instead.

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • You need to add precision and scale when converting to DECIMAL. Otherwise it will use the default values (DECIMAL(18,0)) leading to integer values.

    Try

    SELECT

    ISNULL(CONVERT(DECIMAL(20,2),@val),2) AS [column1]

    FOR XML RAW('row'), elements, root('XMLData'), TYPE



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • Adding (20,2) worked.

    Thanks so much both 😀

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply