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
 Transact-SQL (2000)
 Simple calculation

Author  Topic 

kbearhome
Starting Member

36 Posts

Posted - 2003-09-17 : 17:29:20
I have a table that looks like this:

ProjectNo TotalHours ObsPart
AKNSC000 NULL 109901
alaos000 NULL 261213
ALCGR000 14532 96708
ALCWP000 214866 266653
alfrd000 NULL 281146
AMPKP020 32178 58320
apfbo000 123522 509852
APNBE000 9342 74319
APTBE000 44634 530430

I want to do simple division:

select projectno, totalhours/obspart
from dhb

This is what I get:
AKNSC000 NULL
alaos000 NULL
ALCGR000 0
ALCWP000 0
alfrd000 NULL
AMPKP020 0
apfbo000 0
APNBE000 0
APTBE000 0

Why won't this calculate to anything but zero?

Thanks

izaltsman
A custom title

1139 Posts

Posted - 2003-09-17 : 17:50:42
quote:
Originally posted by kbearhome
Why won't this calculate to anything but zero?



It sees that you are operating with integers, and tries to give you an answer as an integer...
Try something like:

select projectno, cast(totalhours as decimal)/cast (obspart as decimal)
from dhb

or

select projectno, (1.0*totalhours)/obspart
from dhb
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-09-17 : 17:53:27
What would you like NULL / 109901 to return ?

- Jeff
Go to Top of Page

kbearhome
Starting Member

36 Posts

Posted - 2003-09-17 : 17:59:01
I'm happy if null returns null (just showing what the data look like).

Thanks for the info. I was trying to convert the result to decimal but I see that it would need the basic elements converted.
Go to Top of Page

dsdeming

479 Posts

Posted - 2003-09-19 : 08:37:35
quote:
select projectno, (1.0*totalhours)/obspart
from dhb


That's a neat trick. Apparently the multiplication implicitly converts the totalhours integer to a decimal, and there's no need to worry about the denominator at all.

Dennis
Go to Top of Page
   

- Advertisement -