# celling or rounding

### Вопрос

• how to get   5/2 = 2.5 but i wan to round to 3 ..how do i achieve?

9 июня 2012 г. 23:34

### Ответы

• Hi There

Please try this one

`select convert(int,round(convert(decimal(10,2),5)/2,0))`
`select CEILING(convert(decimal(10,2),5)/2)as number`

Many Thanks

Syed Qazafi Anjum

Please click "Mark as Answer" if this resolves your problem or "Vote as Helpful" if you find it helpful.

• Изменено 10 июня 2012 г. 1:18
• Предложено в качестве ответа 10 июня 2012 г. 7:22
• Помечено в качестве ответа 10 июня 2012 г. 18:15
10 июня 2012 г. 1:15
• how to get   5/2 = 2.5 but i wan to round to 3 ..how do i achieve?

I don't think that you need to do the round for this as in T-SQL integer divided by integer return integer.

So for your case it is simple, Select 5/2 + 1 which is 3, no convertion and no rounding.

But I believe you must have a good reason to ask this question, just guess that you want odd number divided by 2 will return back value to be Number/2 + 1, so I think you might look for this:

Select Case when Number % 2 = 1 Then Number/2 + 1 Esle Number/2 End

From your_table

| Blog: MSBICOE.com | MCITP - BI, SQL Developer & DBA

Hate to mislead others, if I'm wrong slap me. Thanks!

• Предложено в качестве ответа 10 июня 2012 г. 7:22
• Изменено 10 июня 2012 г. 8:02
• Помечено в качестве ответа 10 июня 2012 г. 18:15
10 июня 2012 г. 7:12

### Все ответы

• I would recommend to use the

`CEILING () `

function if you always want to round off to the next higher integer - http://msdn.microsoft.com/en-us/library/ms189818.aspx

The FLOOR () function if you always want to round off to the previous lower integer - http://msdn.microsoft.com/en-us/library/ms178531.aspx

AND the ROUND() if you want a decimal result as per your precision requirements - http://msdn.microsoft.com/en-us/library/ms175003.aspx

in your case if you always want 5/2 = 3 , use the CEILING function.

Sanil Mhatre | Database Developer | MCTS | If you find my reply useful in any way, please vote it as helpful. If it has helped answer your question, please mark it as Answer. http://sqlwithsanil.com

9 июня 2012 г. 23:40

• Regards,
Ahmed Ibrahim
SQL Server Setup Team
This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" and "Vote as Helpful" on posts that help you.
This can be beneficial to other community members reading the thread.

9 июня 2012 г. 23:40
• SELECT CEILING(5/2) or  SELECT ROUND(5/2 , 5)  are not working
9 июня 2012 г. 23:45
• can you try

`select ROUND(5.0/2.0, 1)`

or

`select  CAST(5 AS decimal (2,1))  / CAST(2 AS decimal(2,1))`

Regards,
Ahmed Ibrahim
SQL Server Setup Team
This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" and "Vote as Helpful" on posts that help you.
This can be beneficial to other community members reading the thread.

10 июня 2012 г. 1:04
• Hi There

Please try this one

`select convert(int,round(convert(decimal(10,2),5)/2,0))`
`select CEILING(convert(decimal(10,2),5)/2)as number`

Many Thanks

Syed Qazafi Anjum

Please click "Mark as Answer" if this resolves your problem or "Vote as Helpful" if you find it helpful.

• Изменено 10 июня 2012 г. 1:18
• Предложено в качестве ответа 10 июня 2012 г. 7:22
• Помечено в качестве ответа 10 июня 2012 г. 18:15
10 июня 2012 г. 1:15
• how to get   5/2 = 2.5 but i wan to round to 3 ..how do i achieve?

I don't think that you need to do the round for this as in T-SQL integer divided by integer return integer.

So for your case it is simple, Select 5/2 + 1 which is 3, no convertion and no rounding.

But I believe you must have a good reason to ask this question, just guess that you want odd number divided by 2 will return back value to be Number/2 + 1, so I think you might look for this:

Select Case when Number % 2 = 1 Then Number/2 + 1 Esle Number/2 End

From your_table

| Blog: MSBICOE.com | MCITP - BI, SQL Developer & DBA

Hate to mislead others, if I'm wrong slap me. Thanks!

• Предложено в качестве ответа 10 июня 2012 г. 7:22
• Изменено 10 июня 2012 г. 8:02
• Помечено в качестве ответа 10 июня 2012 г. 18:15
10 июня 2012 г. 7:12