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 2008 Forums
 Transact-SQL (2008)
 float type cannot by <1 ?

Author  Topic 

dalibor
Starting Member

21 Posts

Posted - 2009-04-16 : 06:04:52
Hi all,

I have SQL Server 2008 express (with SQL Management studio 10.0.1600.22)
Try this:

declare @pidi float
set @pidi = 1/2 -- 1/1440
print @pidi

Result: 0

Why zero?

Please help, i dont understant why zero.
I am debugging and watching variable @pidi; always only zero, whole time ....

Dalibor

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2009-04-16 : 06:20:47
Because 1 is an integer, not a float. If you do:

declare @pidi float
set @pidi = convert(float,1)/2 -- 1/1440
print @pidi

You will get the correct answer as you are explicitly converting 1 to a float.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-04-16 : 06:26:46
http://sqlblogcasts.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

dalibor
Starting Member

21 Posts

Posted - 2009-04-16 : 06:29:02
Yes, its work.

Every time, when i need assign decimal value to variable, must use set set @variable = convert(float,1)/2 ?

It is complicated, I think, but work it....

Thanks
Go to Top of Page

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2009-04-16 : 06:30:41
Not if your variable is that type already. Read the link Madhivanan posted, I think it will explain the problem better.
Go to Top of Page

dalibor
Starting Member

21 Posts

Posted - 2009-04-16 : 07:52:04
I read the link Madhivanan - very good link.
Now understading.
Thanks !
Go to Top of Page
   

- Advertisement -