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
 Diving two numbers

Author  Topic 

akpbond007
Starting Member

5 Posts

Posted - 2010-06-16 : 03:49:19
Hi
I am using the followng query

select (h7/h8)*100 from <table_name>.

i'm getting values 0 0

h7 and h8 are integers

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2010-06-16 : 04:02:00
Here's 2 ways to do it. The reason you are getting 0 is due to trying to divide an int, an int is a whole number, so doesn't like being divided.

select convert(decimal(18,3),(convert(decimal(18,3),h7)/convert(decimal(18,3),h8))*100) from <table_name>.

select cast((cast(h7 as decimal(18,3))/cast(h8 as decimal(18,3)))*100.00 as decimal(18,3)) from <table_name>.
Go to Top of Page

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2010-06-16 : 04:03:29
quote:
Originally posted by webfred

select (h7/h8)*100.0 from <table_name>


No, you're never too old to Yak'n'Roll if you're too young to die.



Will give you the wrong result as you won't get the remainder in the divide due to h7 and h8 being an int.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-06-16 : 04:04:46
quote:
Originally posted by RickD

quote:
Originally posted by webfred

select (h7/h8)*100.0 from <table_name>


No, you're never too old to Yak'n'Roll if you're too young to die.



Will give you the wrong result as you won't get the remainder in the divide due to h7 and h8 being an int.


Yes I saw my mistake and deleted this ugly post.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-06-16 : 05:25:52
or

select (1.0*h7/h8)*100 from <table_name>

Madhivanan

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

- Advertisement -