May 29, 2010 at 5:32 pm
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
May 29, 2010 at 7:30 pm
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?
May 30, 2010 at 2:52 am
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
May 30, 2010 at 3:24 am
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