Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dividing integer types 3

Status
Not open for further replies.

redsand23

Technical User
Feb 22, 2002
77
US
I am really new to SQL and need help formating a percentage. I want to divide(Always whole numbers) count1 by count2. For example 3/11 = 27.27 then round it to 27.


Here is what I have tried

CONVERT(DECIMAL(18,2),(SUM(count1)/(Sum(count2)))

As you can see I am pretty lost. Any help would be greatly appreciated.
 
CONVERT(DECIMAL(18,2), 1.0 * SUM(count1)/(Sum(count2))


======================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
 
You could also try this if you don't want the 2 decimal places:
Code:
CAST(ROUND(SUM(count1)/SUM(count2),0) as int)

~Brian
 

Works for me...
DECLARE @X DECIMAL(18,2)
SELECT @X = CONVERT(DECIMAL(18,2),(100/11) )
SELECT @X = 100/11
SELECT @X

Looks like you have an extra "(" around "SUM" try...
CONVERT(DECIMAL(18,2),( SUM(count1)/Sum(count2) ))
 
DECLARE @X DECIMAL(18,2)
SELECT @X = CONVERT(DECIMAL(18,2),(100/11) )
SELECT @X = 100/11
SELECT @X

should give 9.00 as it is performing the 100/11 as integer arithmetic before the convert.
CONVERT(DECIMAL(18,2),(1.0 * 100/11) )

gives 9.09.

======================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top