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
 SQL Server Development (2000)
 Select 5/2

Author  Topic 

bjornh
Yak Posting Veteran

87 Posts

Posted - 2002-09-15 : 06:12:01
ok. Here's one that is so simple, that i can't find the answer :D

Select 5/2 gives 2, but I want to get 2.5 as an result.

Does someone know the answer for this guru question?
thx

smccreadie
Aged Yak Warrior

505 Posts

Posted - 2002-09-15 : 07:02:29
You need to use a datatype that allows precision to get the correct answer. If the 5 and 2 are integers, dividing them will give you 2. For example:

 
DECLARE @num1 int, @num2 int
Set @num1 = 5
Set @num2 = 2

SELECT @num1/@num2

Result = 2



If you change to decimal...


 
DECLARE @num1 decimal(9,1), @num2 decimal(9,1)
Set @num1 = 5
Set @num2 = 2

SELECT @num1/@num2

Result = 2.50000000000



HTH

Go to Top of Page
   

- Advertisement -