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

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

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 2012 Update using Select combining 2 rows to get SET value [duplicate]

This question already has answers here:
SQL Server Update Group by
(3 answers)
Closed 6 years ago.
I need to calculate a percentage based on two rows next to each other.
The Select statement finds them and calculates correctly but the Update error says:
Incorrect syntax near the keyword 'Group'
Code:
Update L
Set [TCKR%] = (L.Stock_Close - E.Stock_Close) / L.Stock_Close
From HistData as L
Inner Join HistData as E on L.RecordID = E.RecordID + 1
Where L.RecordID = L.RecordID
Group by L.RecordID, L.Stock_Close, E.Stock_Close
Order by does not work either
I don't think there is a reason to perform a GROUP BY in this case:
UPDATE L
SET [TCKR%] = (L.Stock_Close - E.Stock_Close)/L.Stock_Close
FROM HistData as L
JOIN HistData AS E on L.RecordID = E.RecordID + 1
Note: If Stock_Close is of type int then you have to multiply either the nominator or the denominator by 1.0 so as to avoid integer division.

Not returning Decimal [duplicate]

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)

SQL Server search for number format "ABCABC" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to search for number format "abcabc" in SQL Server
Results can be: 324324,567567, ...
I can do it? What is the Solution?
Please help me, thanks!
1. In the simple case where a value is assumed to be stored as an integer and be in the range of [100,000; 999,999], you can just compare the result of Value / 1000 (which would be an integral division, because both operands are integral) with the result of Value % 1000. The query would look like this:
SELECT Value
FROM dbo.atable
WHERE Value / 1000 = Value % 1000
;
2. If a value can be larger than 999,999 and you want to determine if its decimal representation contains a sequence of digits matching the ABCABC pattern at any position, you could first produce a list each item of which is the result of division of the initial value by a power of 10, the power starting from 0 and going on as long as the quotient is equal to or greater than 100,000. To illustrate that by an example, the following list would be produced for the number 123,456,789:
123456789
12345678
1234567
123456
Next, for each item you would find the result of Item % 1000000, which would be a value with the number of digits no more than 6.
Finally, you would apply to the obtained result the test as in the first case, i.e. Result / 1000 = Result % 1000. A value for which such a match could be found would be included into the output.
To code all the above in Transact-SQL, I would employ a numbers table that includes a 0 and use it like this:
SELECT Value
FROM dbo.atable AS t
WHERE EXISTS (
SELECT *
FROM dbo.Numbers AS n
CROSS APPLY (SELECT t.Value / POWER(CAST(10 AS bigint), n.Number)) AS i (Item)
CROSS APPLY (SELECT i.Item % 1000000) AS r (Result)
WHERE n.Number BETWEEN 0 AND 13 -- 13 is enough to cover the range of a bigint
AND i.Item >= 100000
AND r.Result / 1000 = r.Result % 1000
);
Try this
declare #t table (i int)
insert into #t values (123456),(123654),(0),(null)
select i
from #t
where SUBSTRING(cast(i as varchar),1,1)+1=SUBSTRING(CAST(i as varchar),2,1)
and SUBSTRING(cast(i as varchar),2,1)+1=SUBSTRING(CAST(i as varchar),3,1)
and SUBSTRING(cast(i as varchar),4,1)+1=SUBSTRING(CAST(i as varchar),5,1)
and SUBSTRING(cast(i as varchar),5,1)+1=SUBSTRING(CAST(i as varchar),6,1)

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