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.
Author |
Topic |
sross81
Posting Yak Master
228 Posts |
Posted - 2010-03-08 : 14:12:57
|
I have two columns that I want to divide together and create a new column that displays as a percentage. I can't find an example on how to do that. Can anyone help? Here is my code:select hit, bat, (bat/hit) as 'percent' from ztestsample datebat, hit10,58,66,3I am only getting 0's returnedThanks in Advance!Sherri |
|
namman
Constraint Violating Yak Guru
285 Posts |
Posted - 2010-03-08 : 14:40:29
|
run this code to see how it worksdeclare @ztest table(bat decimal(8,2), hit int)insert into @ztest values(10, 5)insert into @ztest values(8, 5)insert into @ztest values(6, 3)select bat, hit, convert(decimal(8,2), (bat/hit)) as 'percent' from @ztestbat hit percent--------------------------------------- ----------- ---------------------------------------10.00 5 2.008.00 5 1.606.00 3 2.00Another choice is using compouned column, just make sure column hit can never be 0. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-03-09 : 01:18:02
|
select hit, bat, (bat*1.0/hit) as percentfrom ztestMadhivananFailing to plan is Planning to fail |
|
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2010-03-09 : 01:29:28
|
quote: Originally posted by madhivanan select hit, bat, (bat*1.0/hit) as percentfrom ztestMadhivananFailing to plan is Planning to fail
Hi Mr.Madhi...Just a Typo mistakepersent is a keyword..we could an error as Incorrect synatx near the keyword..so it should be'percent'select hit, bat, (bat*1.0/hit) as 'percent'from ztest |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-03-09 : 02:58:59
|
quote: Originally posted by haroon2k9
quote: Originally posted by madhivanan select hit, bat, (bat*1.0/hit) as percentfrom ztestMadhivananFailing to plan is Planning to fail
Hi Mr.Madhi...Just a Typo mistakepersent is a keyword..we could an error as Incorrect synatx near the keyword..so it should be'percent'select hit, bat, (bat*1.0/hit) as 'percent'from ztest
Ok. It is a keyword. Better useselect hit, bat, (bat*1.0/hit) as [percent]from ztestMadhivananFailing to plan is Planning to fail |
|
|
|
|
|
|
|