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
 Divide value showing 0.

Author  Topic 

hspatil31
Posting Yak Master

182 Posts

Posted - 2011-12-23 : 09:30:03
Dear All,

I am trying following statement but I am getting result 0.
Actually it should shows 0.9.
Can anybody please tell me how to resolve this issue ?

declare @Duration_RoundedToMinute real;
SELECT @Duration_RoundedToMinute = 54/60
print @Duration_RoundedToMinute

Thanks and Regard's
Harish Patil

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-12-23 : 09:37:43
This is because of integer division. "If an integer dividend is divided by an integer divisor, the result is an integer that has any fractional part of the result truncated."

Force it to a non-integer type by doing one of these:
declare @Duration_RoundedToMinute real;
SELECT @Duration_RoundedToMinute = 54.0/60
print @Duration_RoundedToMinute
declare @Duration_RoundedToMinute real;
SELECT @Duration_RoundedToMinute = CAST(54 AS FLOAT)/60
print @Duration_RoundedToMinute
Go to Top of Page

hspatil31
Posting Yak Master

182 Posts

Posted - 2011-12-23 : 09:44:26
Hello Friend,

Thnks. This value I am inserting in database table. But there Field datatype is bigint.
Can you please tell me how to insert this value into database field.

Thanks and Regard's
Harish Patil
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-12-23 : 09:50:56
54*1.000/60*1.000

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2011-12-23 : 09:55:09
If the field is BigInt, you cannot insert it. You need to change the field to DECIMAL(18,2) or some such.

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-12-27 : 08:17:47
Also read this post
http://beyondrelational.com/blogs/madhivanan/archive/2008/01/16/beware-of-implicit-conversions.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -