SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Adding zeros after decimal to float data type
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

capella07
Starting Member

USA
46 Posts

Posted - 02/29/2008 :  10:20:02  Show Profile  Visit capella07's Homepage  Reply with Quote
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
Flowing Fount of Yak Knowledge

India
5581 Posts

Posted - 02/29/2008 :  10:24:59  Show Profile  Visit harsh_athalye's Homepage  Click to see harsh_athalye's MSN Messenger address  Send harsh_athalye a Yahoo! Message  Reply with Quote
CAST(SUM(pd.nettime), numeric(10,2))


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

capella07
Starting Member

USA
46 Posts

Posted - 02/29/2008 :  10:38:51  Show Profile  Visit capella07's Homepage  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000