Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
if value is 75.5 or more i want to return 76 and less than 75.5 I want to return 75.

This is what my query looks like

SELECT DISTRICT, 
COUNT(DISTRICT) RegionDistrict,
CEILING(SUM(CONVERT(int,[turnoverSM (%)]))/COUNT(DISTRICT)) as SM,
CEILING(SUM(CONVERT(int,[turnoverASM (%)]))/COUNT(DISTRICT)) as ASM
FROM HRStaffTable WHERE REGION = 40 AND Created_Date = CONVERT(DATE, GetDate()) GROUP BY DISTRICT


What I have tried:

SELECT CEILING(1060/14) as result
Posted
Updated 9-Feb-18 3:18am
Comments
Ziee-M 9-Feb-18 9:23am    
You can create your own function that Rounds using your own rule

1 solution

When you divide two integers, the result will always be truncated. If you want to round the result instead, then one of the values needs to be a floating-point type.
SQL
ROUND(CONVERT(real, SUM([turnoverSM (%)])) / COUNT(DISTRICT), 0)

ROUND (Transact-SQL) | Microsoft Docs[^]

Also, in case you haven't realised it yet, it's an extremely bad idea for table or column names to contain spaces or other special characters. If possible, you should redesign your database to rename the columns so that they only use alphanumeric characters.
 
Share this answer
 
v2
Comments
istudent 9-Feb-18 9:22am    
I tested Select ROUND((1060/14),0) as Result , it still return 75
Richard Deeming 9-Feb-18 9:24am    
Because both 1060 and 14 are integers!

Try: ROUND(1060. / 14, 0) - note the "." at the end of the first number.
istudent 9-Feb-18 9:38am    
But there is no way for me to add decimal in my field in table. How do I achieve that? if you checked the column SUM([turnoverSM (%)]) is 1060
Richard Deeming 9-Feb-18 9:41am    
OK, I thought it was a floating-point type, since you're explicitly converting it to int.

Try converting it to double instead:
ROUND(CONVERT(double, SUM([turnoverSM (%)])) / COUNT(DISTRICT), 0)
istudent 9-Feb-18 9:47am    
I convert it to decimal, it return 76.000000, it worked but I think I should just return the sum and do calculation in code.

like this.

Dim CriticalIndicator = "1060"
Dim districtstorecount = "14"

Dim ci As Int32 = Convert.ToInt32(CriticalIndicator)
Dim dc As Int32 = Convert.ToInt32(districtstorecount)
Dim HelloPercentage = (ci / dc)


Console.WriteLine(String.Format("{0:N0}", HelloPercentage))

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900