Round Up SQL SERVER

Asked

Viewed 4,510 times

2

I’m making a SUM(x) and I need to divide it by 8, for this example, my result will be 5.5. The result needs to be like an INT and rounded up.. So for this example, I need the result 6.

I’ve already tried:

ROUND(SUM(x)/8,0) > I obtained 5

ROUND(SUM(x)/8,-1) > I obtained 10

CEILING(SUM(x)/8) > I obtained 5

I’m doing this, the result of the SUM of the 44:

SELECT ROUND(SUM(x)/8,0) FROM Tabela

If you do it with the direct value, it rounds right:

SELECT ROUND(44/8,0) FROM Tabela

But I need it to be with SUM!

  • CEILING ( numeric_expression ) should work. https://docs.microsoft.com/pt-br/sql/t-sql/functions/ceiling-transact-sql?view=sql-server-2017

  • Like I said, I’ve tried..

2 answers

3


Knife like this :

 SELECT CONVERT(DECIMAL,ROUND(SUM(44)/8.0,0))

You are putting the comma in place of the point. So he understands that it is another parameter not the number.

  • 1

    Your answer didn’t help me at first, but I tried to do something I saw you used that I didn’t use and it worked : CEILING(SUM(elrSP18)/8.0) .0 after the 8 and magically rounded up....

0

Is this what you’re looking for? I believe you’re doing the right thing, but rounding up would be using the CEILING function.

I made an example where: Somapura is the sum of my values. Valuemove is the SUM divided by 2 rounded up. Somapelatabeladivdois is the SUM of the table divided by 2 rounded up.

DECLARE @T1 TABLE (
valor float
)

INSERT INTO @T1 VALUES (1.9)
INSERT INTO @T1 VALUES (2.3)
INSERT INTO @T1 VALUES (3.4)
INSERT INTO @T1 VALUES (1.8)

SELECT SUM(valor) AS SomaPura FROM @T1

select CEILING(9.4/2) AS ValorDivDois -- 5

SELECT CEILING(SUM(valor)/2) AS SomaPelaTabelaDivDois FROM @T1 -- 5

I hope it helps.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.