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 2005 Forums
 Transact-SQL (2005)
 calculation problem

Author  Topic 

zubair
Yak Posting Veteran

67 Posts

Posted - 2009-12-14 : 17:04:44
Hi I have the following....

select (convert(float,(50 - 40 / 40))) as test

when i output the value it is 49.0. Can anyone explain to me why its not 0.25 as thats what the value should be after doing the calculation!?

Thanks

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-14 : 17:10:17
Because you are using integers, so you get an integer back. Instead do this: select (50 - 40)*1.0/40 as test


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

zubair
Yak Posting Veteran

67 Posts

Posted - 2009-12-14 : 17:37:46
Thanks for the reply... just another thing.. for the following

select (50 - 70)*100/70 as test

I get an answer of -28

However hen i do the calculation on a calculator i get a value of -28.57 rounded to 2 decimal places. Is there a way to get the decimal values as well?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-14 : 17:44:12
Do you see in my post how it says 1.0? You need to use that trick to get what you want.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-12-14 : 17:47:07
This will be a good read..

http://sqlblogcasts.com/blogs/madhivanan/archive/2008/01/16/beware-of-implicit-conversions.aspx
Go to Top of Page

zubair
Yak Posting Veteran

67 Posts

Posted - 2009-12-14 : 17:50:17
Thanks Tara. did the trick!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-14 : 17:51:50
You're welcome.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -