Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Hi I am using the followng queryselect (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>.
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.
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.
madhivanan
Premature Yak Congratulator
22864 Posts
Posted - 2010-06-16 : 05:25:52
orselect (1.0*h7/h8)*100 from <table_name>MadhivananFailing to plan is Planning to fail