Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Adding zeros after decimal to float data type

Author  Topic 

capella07
Starting Member

46 Posts

Posted - 2008-02-29 : 10:20:02
A column (called "nettime") I'm pulling from a table (payr_paydetail) is of the float data type (size is 8). I need to use the SUM function on that column AND (here's the problem part that I can't figure out) I need to have up to two zeros after the decimal, regardless of whether the result is a whole number, or one digit after the decimal. I.e. if the SUM of the column is an even 108, then the result should be 108.00, not 108, or 108.0, or 108.0000.

The basic query looks like this:
SELECT
SUM(pd.nettime) as HoursWorked
FROM dbo.payr_PayDetail pd
WHERE (pd.statusid = 12)


Now, I've spent a lot of time on this and I've tried a number of things, all to no avail. Here's a list of what I've tried:

CAST(ROUND(SUM(pd.nettime),2) as float) AS HoursWorked
CAST(SUM(ROUND(pd.nettime,2)) as varchar) AS HoursWorked
CAST(SUM(pd.nettime) AS dec(4,2)) AS HoursWorked
CONVERT(dec(4,2), (SUM(pd.nettime))) AS HoursWorked
CAST(CONVERT(dec(4,2), (SUM(pd.nettime))) AS varchar) AS HoursWorked


The last three lines above all generate the error, "Arithmetic overflow error converting float to data type numeric." It's my understanding (and I'm reeeeeeaaaaaaal new to to SQL, so correct me if I'm wrong!) that the reason for the error is that in those three instances I'm trying to convert a float data type with a higher precision to the decimal data type which is of a lower precision.

I (and my boss who wanted this done yesterday!) would greatly appreciate it if someone could help me out here.

Thanks in advance!


=====================================
f u cn rd ths, u cn gt a gd jb n prgrmng

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-02-29 : 10:24:59
[code]CAST(SUM(pd.nettime), numeric(10,2))[/code]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

capella07
Starting Member

46 Posts

Posted - 2008-02-29 : 10:38:51
harsh_athalye, THANK YOU SOOOO MUCH!

Works like a charm.

One thing though: I copy pasted your line & got an error saying "numeric" isn't a recognised function - I just replaced the commma before it with "AS".

Thanks again...


=====================================
f u cn rd ths, u cn gt a gd jb n prgrmng
Go to Top of Page
   

- Advertisement -