szadroga
asked on
CONVERT number to decimal with SQL
I have the following SELECT statement (see code) and I am doing a division calculation (sumscore1 / count_deptname). The math works, but it returns a whole number. I would like the number to have one decimal place.
How do I CONVERT that calculation within the SELECT statement?
How do I CONVERT that calculation within the SELECT statement?
SELECT DepartmentName, SumScore1, SumScore1 / Count_DeptName AS AverageScore1, SumScore2, SumScore3, Count_DeptName
FROM vwDepartmentSummary
In your case:
SELECT DepartmentName, SumScore1, Cast(SumScore1 / Count_DeptName as decimal(10,1)) AS AverageScore1, SumScore2, SumScore3, Count_DeptName
FROM vwDepartmentSummary
/Marten
SELECT DepartmentName, SumScore1, Cast(SumScore1 / Count_DeptName as decimal(10,1)) AS AverageScore1, SumScore2, SumScore3, Count_DeptName
FROM vwDepartmentSummary
/Marten
ASKER
What does the (10,1) represent. i assume the 1 represents the number of decimal places, but what does the 10 represent in the parameters?
ASKER
I am still not getting the proper calculation. The calculation occurring is 14 / 5 and I am getting 2.0, instead of 2.8 ... Is there something i am doing wrong in the calculation, do i need to write a different function?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I'm sorry, but I'm not happy with the way you split the points.
1) You accepted a solution that is not valid (comment 24375667 is not giving you the result you wanted)
2) I have provided you an alternative and I even answered you what the 10 in "decimal(10,1)" means.
I feel I should have received some points. Asker, can you please review this question?
Thanks
1) You accepted a solution that is not valid (comment 24375667 is not giving you the result you wanted)
2) I have provided you an alternative and I even answered you what the 10 in "decimal(10,1)" means.
I feel I should have received some points. Asker, can you please review this question?
Thanks
ASKER
Did I not split the points evenly? How do I go about re-assigning points?
Click on the "request attention" link and ask for the points to be reassigned.
I'd have to agree with ralmada here. I believe that a 4x split is the fairest.
+1
(Means agreeing with the speaker above, i e BrandonGalederisi)
/Marten
(Means agreeing with the speaker above, i e BrandonGalederisi)
/Marten
ASKER
I am only seeing 3 different responders...am I missing something?
3x split... sorry... here is MY opinion.
http:#a24375866 by ralmada - 250 for the first working solution
http:#a24376023 by brandongalderisi - 125 for my post
http:#a24376411 by martenrune - 125 for fixing my above post to limit decimal points
marten... none of your previous posts prior to the one I reference will perform the math correctly since the result will be an integer, cast to an decimal.
http:#a24375866 by ralmada - 250 for the first working solution
http:#a24376023 by brandongalderisi - 125 for my post
http:#a24376411 by martenrune - 125 for fixing my above post to limit decimal points
marten... none of your previous posts prior to the one I reference will perform the math correctly since the result will be an integer, cast to an decimal.
ASKER
Tried to divide as evenly as possible. thanks for all the help and quick responses
I think that is the fairest split given timeliness and accuracy.
declare @int1 as integer
declare @int2 as integer
set @int1 = 5
set @int2 = 3
select cast(@int1 / @int2 as decimal(10,2))