SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Select Decimal


Select Decimal

Author
Message
azzamein
azzamein
SSC Rookie
SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)

Group: General Forum Members
Points: 26 Visits: 12
On an SQL 2005 Server I have a table column filled with float values. What I want to do is sum up only the decimal portion of each value. For example, for the values:



160.2

100

79.5



The sum would be .7 (.5 + .2)



My problem is that I am told Decimal is not a valid function, and I really don't want to have to loop through each value in code to pull out/add the decimals.



Is there a way to do this in SQL 2005?
Andre St-Onge
Andre St-Onge
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 22
Just use Round function







IF NOT OBJECT_ID('ANDRETEST') IS NULL

DROP TABLE ANDRETEST

GO



CREATE TABLE ANDRETEST

(

F1 FLOAT,

F2 CHAR(10)

)

GO





INSERT INTO ANDRETEST ( F1, F2 ) VALUES ( 160.2,'TEST')

INSERT INTO ANDRETEST ( F1, F2 ) VALUES ( 100,'TEST')

INSERT INTO ANDRETEST ( F1, F2 ) VALUES ( 79.5,'TEST')







SELECT ROUND(SUM(F1 - ROUND( F1, 0, 1)), 1) AS TEST FROM ANDRETEST



IF NOT OBJECT_ID('ANDRETEST') IS NULL

DROP TABLE ANDRETEST

GO
David Webb-CDS
David Webb-CDS
SSCrazy Eights
SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)

Group: General Forum Members
Points: 8232 Visits: 8587
You cound try something like:



create table #junk (

float_to_add float NOT NULL)

go

insert #junk(float_to_add) values (1.1104)

insert #junk(float_to_add) values (1.222)

insert #junk(float_to_add) values (1.32)

insert #junk(float_to_add) values (1.41)

insert #junk(float_to_add) values (1.52)

insert #junk(float_to_add) values (1.66)

insert #junk(float_to_add) values (1.71)

go



select float_to_add from #junk

go

select sum(convert(numeric(13,3),float_to_add) % 1.000) from #junk

go



drop table #junk

go





But float is not an exact datatype, it's an approximation, so you may get some unexpected results because a number like 1.3 might actually come out as 1.29999999999.



If the numbers you're storing need definite precision, float may not be the appropriate data type.



And then again, I might be wrong ...
David Webb
azzamein
azzamein
SSC Rookie
SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)

Group: General Forum Members
Points: 26 Visits: 12
SUM(F1 - ROUND( F1, 0, 1) worked great. Thank you.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search