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.
Author |
Topic |
sabirpatel
Starting Member
22 Posts |
Posted - 2004-11-24 : 05:16:31
|
Hi,The problem seems to be very simple but i could not find out the Solution.select cast(5/2 as float)The result of the above query should be 2.5 but instead it displays 2.0. It round's of the decimal. However query select (5*0.5) works fine. I have also used convert and tried but still the same result.Please help me out how to get a decimal output while dividing.Thanking you in advance for your help. RegardsSabirSabir Patelemail: sabirpatel@gmail.com |
|
jen
Master Smack Fu Yak Hacker
4110 Posts |
Posted - 2004-11-24 : 05:23:17
|
5 and 2 are considered as integer. use 5.0 and 2.0 instead or convert them to float or decimal : )--------------------keeping it simple... |
|
|
sabirpatel
Starting Member
22 Posts |
Posted - 2004-11-24 : 06:37:41
|
Yes it worked. Thanks a lot for your help...Sabir Patelemail: sabirpatel@gmail.com |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-11-24 : 08:49:39
|
Just remember that like any mathematical expression, there is an order of operations that applies:Step 1: select cast(5/2 as float)First, SQL divides the integer 5 by the integer 2. the result of that will be an integer, so it gets rounded to 2.0Step 2: select cast(2 as float)So, 2 then gets converted to a float. The result -- 2.0 !--If you are dealing with column names or variables and not constants that you can just change from 5 to 5.0, you just need to force the convert or cast to happen little earlier:select cast(5 as float) / 2- Jeff |
|
|
elwoos
Master Smack Fu Yak Hacker
2052 Posts |
Posted - 2004-11-24 : 17:31:16
|
Would SQL server always round .5 down then in these circumstances? I'm suprised the answer to select cast(5/2 as float) isn't 3.0 but then it is a very long time since I've done this sort of stuff.steveTo alcohol ! The cause of - and solution to - all of life's problems |
|
|
jen
Master Smack Fu Yak Hacker
4110 Posts |
Posted - 2004-11-25 : 01:28:11
|
i think (not sure), it truncates the resulting value to a whole number and doesn't evaluate the fractional part anymore. 7/4 returns 1 even if you get 1.75--------------------keeping it simple... |
|
|
|
|
|