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 2005 Forums
 Transact-SQL (2005)
 Convert to %

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 ztest


sample date
bat, hit
10,5
8,6
6,3

I am only getting 0's returned
Thanks in Advance!
Sherri

namman
Constraint Violating Yak Guru

285 Posts

Posted - 2010-03-08 : 14:40:29
run this code to see how it works

declare @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 @ztest


bat hit percent
--------------------------------------- ----------- ---------------------------------------
10.00 5 2.00
8.00 5 1.60
6.00 3 2.00




Another choice is using compouned column, just make sure column hit can never be 0.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-03-09 : 01:18:02
select hit, bat, (bat*1.0/hit) as percent
from ztest

Madhivanan

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

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 percent
from ztest

Madhivanan

Failing to plan is Planning to fail



Hi Mr.Madhi...

Just a Typo mistake
persent 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
Go to Top of Page

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 percent
from ztest

Madhivanan

Failing to plan is Planning to fail



Hi Mr.Madhi...

Just a Typo mistake
persent 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 use

select hit, bat, (bat*1.0/hit) as [percent]
from ztest

Madhivanan

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

- Advertisement -