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
 General SQL Server Forums
 New to SQL Server Programming
 Which Datatype to Use?

Author  Topic 

sqlslick
Yak Posting Veteran

83 Posts

Posted - 2012-11-29 : 08:53:10
Hey guys,

I have 2 variables below:

@Target (FLOAT)
@Days (INT)

@Days = 182

I am trying to do assign the following calculation to @Target:

@Target = 6/365*@Days

The arithmetic operation above equals a value of 2.99178082 but I need it to display as 3.0 only. However, I keep getting 0 only and I'm not understanding why. I think it is the Datatype I am using but I have tried DECIMAL(3, 3) without success.

Please help!

Thanks,
John

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2012-11-29 : 08:57:12
@Target = 6/(365.0)*Days

(OR)

@Target = 6.0/365*Days

--
Chandu
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-11-29 : 08:59:27
This is because of INTEGER division. Force it to be floating point by doing one of the following:
@Target = 6E/365*@Days
@Target = 6.0/365*@Days

You can use ROUND function or cast to decimal ( decimal(18,0) for example) to round off the decimal places.
Go to Top of Page

sqlslick
Yak Posting Veteran

83 Posts

Posted - 2012-11-29 : 09:01:14
Wow, that easy eh? I just applied the ROUND() function too and it works perfect!!!

Thank you so much!!!
John
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-11-29 : 09:25:03
Yes:)

More often than not, I end up using CAST to decimal rather than ROUND because of the seemingly rational, but very irrational behavior of ROUND function under certain conditions. If you are interested, the gory details are here:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=158176
Go to Top of Page
   

- Advertisement -