![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
SELECT CEILING(5/2) AS OOPS, CEILING(5.0/2) AS HMMM FROM Schools In my part of the world CEILING(5/2) and CEILING(5.0/2) do not evaluate to the same quantity in MS-SQL Server. As I see it, for CEILING(5/2),noting that 5 and 2 are whole numbers, SQL decides to truncate the result of the division, returning 2, not 2.5. The ceiling of 2 is 2. When we introduce 5.0 SQL decides to return a fractional amount, 2.5; The ceiling of 2.5 is 3. I search my SQL regularly and write my CEILING (and any other functions that might be influenced) functions as CEILING(Cast(a as float) / b). This situation bites me occasionally until I realize what I have done. Perhaps you have a comment or a simpler or more global solution. |
#3
| |||
| |||
|
|
Tom van Stiphout <no.spam.tom7744 (AT) cox (DOT) net> wrote in news:rbrfn3pbruu5aavk937qb4d0fmg4r1gcfp (AT) 4ax (DOT) com: From Books Online, on the Divide Operator: If an integer dividend is divided by an integer divisor, the result is an integer that has any fractional part of the result truncated. Thus: SELECT 5/2 = 2 So, you have to cast any division that uses whole numbers? Is there some way to force it without CAST? |
#4
| |||
| |||
|
|
So, you have to cast any division that uses whole numbers? Is there some way to force it without CAST? |
#5
| |||
| |||
|
|
But when it's a variable Casting the Dividend (or Converting it) as some non-integer number type is the only way I've found. I'd love to find something like (SET ANSI_NULLS ON) that would make division behave like most programming languages with which I'm familiar, but I haven't. |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |