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
 division operator

Author  Topic 

allan8964
Posting Yak Master

249 Posts

Posted - 2013-05-31 : 13:11:26
Hi there,

This is a very beginner's question:
I have @var1=13, @var2=4, and How can I calculate @var3=@var1/@var2.
Thanks in advance.

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-05-31 : 13:24:42
[code]SET @var3 = @var1/@var2; -- integer division; result = 3
SET @var3 = 1.0*@var1/@var2;-- floating point division; result = 3.25[/code]
Go to Top of Page

allan8964
Posting Yak Master

249 Posts

Posted - 2013-05-31 : 15:00:41
Thanks James. That's what I need. Further, if I need floating point result, can I set @var1 and @var2 other than int to get this?
Go to Top of Page

allan8964
Posting Yak Master

249 Posts

Posted - 2013-05-31 : 16:19:32
Did some tests and found that I must keep @var1 and @var2 int and the @var3 floating. So I can use cast like this:

declare @var3 float;
set @var3 = CAST(@var1 AS float) / CAST(@var2 AS float);

It works.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-01 : 05:10:14
quote:
Originally posted by allan8964

Did some tests and found that I must keep @var1 and @var2 int and the @var3 floating. So I can use cast like this:

declare @var3 float;
set @var3 = CAST(@var1 AS float) / CAST(@var2 AS float);

It works.


thats the same think James did implicitly by multiplying 1.0

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -