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)
 Can we write direct query to get % of 2 columsn

Author  Topic 

satish.gorijala
Posting Yak Master

182 Posts

Posted - 2009-02-03 : 00:35:16

col1 col2 col3
4 5
6 7

Can we get col3 as 4/5*100, 6/7*100?
I need col3 as Percentage value of col1 & col2?
Can we have direct query to do this, if the table contains...lakhs of records?

G. Satish

thiyagu_rind
Starting Member

46 Posts

Posted - 2009-02-03 : 00:48:18
hi
something like this.
SELECT col1,col2,col1/col2*100 as percentage from tablenameor
we can add a constraints for the col3 with the help of ALTER statement.

Regards
Thiyagarajan
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-02-03 : 01:01:13
update t
set col3 = col1/col2*100
from tablename t
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-03 : 09:26:43
quote:
Originally posted by bklr

update t
set col3 = col1/col2*100.0
from tablename t



small modification
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-03 : 09:27:39
see reason here
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/01/16/beware-of-implicit-conversions.aspx
Go to Top of Page
   

- Advertisement -