Not returning Decimal [duplicate] - sql-server

This question already has answers here:
Integer division in sql server
(8 answers)
Closed 7 years ago.
Can anyone tell me why this is not returning a decimal- only Zeroes.
Set PercentComplete = (CSCCalled + HospCalled + NoCalls)/(TotalInventory)
CSCCalled is an Integer
HospCalled is an Integer
NoCalls is an Integer
TotalInventory is an Integer
In the TempTable- PercentComplete is Decimal(9,3)
The results end up either being a 1 or 0, not a decimal. Does anyone have any suggestions?
Thanks,
Scott

You can cast one value to float in order to get a float result or you can add a float constant to promote every integer into a float. Try one of the following:
Set PercentComplete = 1.0 * (CSCCalled + HospCalled + NoCalls)/(TotalInventory)
or
Set PercentComplete = (CAST(CSCCalled AS FLOAT) + HospCalled + NoCalls)/(TotalInventory)

Related

Percentage value not being shown in sql server [duplicate]

This question already has answers here:
SQL Server, division returns zero
(6 answers)
Division of integers returns 0
(2 answers)
Closed 4 months ago.
I want to calculate the percentage change of some statistics id using SQLSEVER.
I want to calculate %change using = ((curr-prev)/prev) * 100
My %change expected there was 2.63% since (400008/15189461)* 100 but I am getting result as 0.00%.
select
c.poscount as curr,
p.poscount as 'prev',
(p.PosCount - c.PosCount) 'difference',
CONVERT(
decimal(2, 2),
(p.PosCount - c.PosCount)/ NULLIF(p.PosCount, 0)
)   as   'Poscountchange'
from
(
select
*
from
Goker.dbo.LocalStatisticsDetail_daily
where
statisticid = '13527'
)   c
inner join (
select
*
from
Goker.dbo.LocalStatisticsDetail_daily
where
statisticid = '13373'
) p on c.fieldname = p.fieldname
Why I am getting 0% as percentage change? I didn't wanted to change to decimal at first and my result was 0% before converting it into decimal.

Sql Server is rounding the value while calculating average [duplicate]

This question already has answers here:
Integer division in sql server
(8 answers)
Decimal values in SQL for dividing results
(6 answers)
Closed 2 years ago.
I am trying to calculate average and the apply CEILING on the average, but somehow when i calculate average it is always rounding off the value.
DECLARE #AverageStudents INT
SELECT #AverageStudents = CEILING((SELECT #TotalUsersInAllScools + #TotalUsersInAllColleges AS FLOAT) + (SELECT #TotalSchools + #TotalUsersColleges AS FLOAT))
SELECT #AverageStudents AS 'AverageStudents'
In my case #TotalUsersInAllScools + #TotalUsersInAllColleges = 875 , and #TotalSchools + #TotalUsersColleges = 216
875/216 = 4.0509 and CEILING of it should give me output 5 but its giving 4.
What i observed is #TotalUsersInAllScools + #TotalUsersInAllColleges AS FLOAT) + (SELECT #TotalSchools + #TotalUsersColleges AS FLOAT)is giving output as 4

SQL Server how to get money type's decimal digits? [duplicate]

This question already has answers here:
Get the number of digits after the decimal point of a float (with or without decimal part)
(6 answers)
Closed 4 years ago.
The column CostPrice of table t1 is money type.
The data in column CostPrice likes this:
141.1938
0.00
147.1041
119.592
1.23
I use this sql to get the decimal digits:
select distinct len(CostPrice-floor(CostPrice))-2 from t1;
But the result is only 2,this is not right,in fact,the result should be 2,3,4.
So how to fix the sql?
Added:
StackOverflow does not allow me to flag the flagging but asked me to edit the question instead, so:
This Question is not a duplicate to this one. The existing question is on the "float" datatype and this one is on "money", Converting "money" to "varchar" like in the "existing anser" will always return the same number of decimal places, so it does not answer this question.
You could (independantly from regional settings):
multiply the value by 10,000
convert the result to integer
convert the integer to a string
add 4 leading zeroes to the left (just in case...)
take the 4 characters from the right (the former decimal places)
replace each zero by a blank character
remove the trailing blanks using rtrim
return the length of the remaining string
To put this in an expression, it would be:
LEN(RTRIM(REPLACE(RIGHT('0000' + CONVERT(varchar(20), CONVERT(int, CostPrice*10000)), 4), '0', ' ')))
Try this
DECLARE
#money money = 141.1938
SELECT
#money
,SUBSTRING(CONVERT(varchar(20),#money,2), CHARINDEX('.', CONVERT(varchar(20),#money,2)) + 1, LEN(CONVERT(varchar(20),#money,2))) as RESULT

SQL Server calculation returning 0 instead of a float value

I am trying to do the following operation in SQL to calculate the weighted rating :
SELECT CTE_3.idProduct,(CTE_3.vote_count / (CTE_3.vote_count + #minimumVotesRequired)) * CTE_3.vote_mean + (#minimumVotesRequired / (CTE_3.vote_count+ #minimumVotesRequired)) * ((SUM(CTE_3.vote_mean)/COUNT(CTE_3.IdProduct))) AS WeightedRating
FROM CTE_3
GROUP BY CTE_3.IdProduct,
CTE_3.vote_count,
CTE_3.vote_mean
ORDER BY idProduct;
But the problem I am facing is that the result is ALWAYS 0.. I tried using Convert(FLOAT,operation) AS WeightedRating but still I am getting a result of 0.
When I manually perform this on a calculator it returns 2.5416666..so I am quite sure that SQL Server is not being able to manage the values I feed to the operation.
Should I do something else than cast?
The values are :
vote_count is 2
vote_mean is 2.5
#minimumVotesRequired is 1
EDIT :
Now the only value after casting everything to float is 2.5 from CTE_3.vote_mean
SELECT CTE_3.idProduct,(CONVERT(FLOAT,CTE_3.vote_count) / (CONVERT(FLOAT,CTE_3.vote_count) + #minimumVotesRequired))
* CONVERT(FLOAT,CTE_3.vote_mean) +
(#minimumVotesRequired / (CONVERT(FLOAT,CTE_3.vote_count)+ #minimumVotesRequired))
* (SUM(CONVERT(FLOAT,CTE_3.vote_mean)))/COUNT(CTE_3.IdProduct)) AS WeightedRating
FROM CTE_3
GROUP BY CTE_3.IdProduct,
CTE_3.vote_count,
CTE_3.vote_mean
ORDER BY idProduct;
Any suggestion in what am I missing?
If CTE_3.vote_count type is int or #minimumVotesRequired type is int, then you are getting the truncated int value. Make sure those are floats, or cast them as floats before doing your division.
Also, don't forget that COUNT is an integer function. You will want to cast the result of your COUNT as a float as well.

function to get only the integer [duplicate]

This question already has answers here:
Issue with Round function in ssrs
(3 answers)
Closed 8 years ago.
What function I have to use to get as a Result 1 in the following expression, in SQL Server or SSRS please
select ROUND((3 - (4 * 0.32)), 1) = 1.70
FLOOR is the function that you need: http://msdn.microsoft.com/en-us/library/ms178531.aspx
select FLOOR(ROUND((3 - (4 * 0.32)), 1))
ROUND((3 - (4 * 0.32)), 0)
=> 2
ROUND()
If that is not the desired result then you probably want to use FLOOR

Resources