FOR XML issue

guest2013-1

guest
Joined
Aug 22, 2003
Messages
19,800
I have the following

Code:
declare @perc float

set @perc = 75

select @perc

select @perc FOR XML RAW, TYPE, ELEMENTS

If you execute it you get:

Code:
75

<row>7.500000000000000e+001</row>

WTF on the <row> :confused:

Why is it doing that? How can I correct it? I need to use float values in my select statements that will go into XML format like that. But I can't afford the data being different when you output it as XML...

The money type field works perfectly, but I don't want to "money" all my floats.... just not right...
 

dequadin

Expert Member
Joined
May 9, 2008
Messages
1,434
Have you tried casting?
Code:
SELECT CONVERT(varchar(100), CAST(@perc AS decimal(38,2)))

Found that here
 

guest2013-1

guest
Joined
Aug 22, 2003
Messages
19,800
I don't convert it to varchar, I did see those result but I figured "no, I'm using FOR XML and it's outputting XML, not trying to convert it".

Why decimal though? What happens to fields that's float? Would you say that as a standard I need to start implementing decimal instead of float?
 

Shred

Expert Member
Company Rep
Joined
Jul 12, 2006
Messages
1,736
Floats can be funny things. I would just use decimal from the start.
 

FarligOpptreden

Executive Member
Joined
Mar 5, 2007
Messages
5,396
I always, always, ALWAYS use decimal. Can't remember the last time I used float...

EDIT: Just a question though, why use FOR XML "RAW" and not "AUTO" or "EXPLICIT"? You have a lot more control over the element / attribute output in EXPLICIT and AUTO output modes...
 

guest2013-1

guest
Joined
Aug 22, 2003
Messages
19,800
I always, always, ALWAYS use decimal. Can't remember the last time I used float...

EDIT: Just a question though, why use FOR XML "RAW" and not "AUTO" or "EXPLICIT"? You have a lot more control over the element / attribute output in EXPLICIT and AUTO output modes...

I use it so that I can parse it easier in my one stored procedure that converts the XML to json format. I want something simple and quick, and don't need to mess around too much with the data. So if a table can be quickly outputted to <row><your>Mother</your></row> by just a simple select. I'm happy.
 
Top