dbTalk Databases Forums  

MS-SQL Server and Math

comp.databases.ms-access comp.databases.ms-access


Discuss MS-SQL Server and Math in the comp.databases.ms-access forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
lyle
 
Posts: n/a

Default MS-SQL Server and Math - 12-30-2007 , 01:10 PM






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.




Reply With Quote
  #2  
Old   
Tom van Stiphout
 
Posts: n/a

Default Re: MS-SQL Server and Math - 12-30-2007 , 02:13 PM






On Sun, 30 Dec 2007 10:10:55 -0800 (PST), lyle
<lyle.fairfield (AT) gmail (DOT) com> wrote:


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


Quote:
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.



Reply With Quote
  #3  
Old   
Tom van Stiphout
 
Posts: n/a

Default Re: MS-SQL Server and Math - 12-30-2007 , 04:38 PM



On 30 Dec 2007 20:27:48 GMT, "David W. Fenton"
<XXXusenet (AT) dfenton (DOT) com.invalid> wrote:

When you're using Integer columns you'll have to CAST or CONVERT. In
trivial cases you can write:
select 5.0/2
-> 2.500000

It's just one of those things you learn the hard way.

-Tom.


Quote:
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?

Reply With Quote
  #4  
Old   
lyle
 
Posts: n/a

Default Re: MS-SQL Server and Math - 12-30-2007 , 04:45 PM



On Dec 30, 3:27 pm, "David W. Fenton" <XXXuse... (AT) dfenton (DOT) com.invalid>
wrote:

Quote:
So, you have to cast any division that uses whole numbers? Is there
some way to force it without CAST?
With a literal you can just add a decimal point and a zero as 5.0/2
rather than 5/2.

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.

This has bitten me three times, in 2005, 2006, 2007. And I've learned
it three times. Then I forget about it. Then a year later it hits me
again, and I'm trying to explain why there we're one pizza short.

I don't know how 5/2 works in a Pass Through Query sent through ODBC
to MS-SQL from Access but I'm working directly in MS-SQL so that's
only mildly interesting to me.




Reply With Quote
  #5  
Old   
CDMAPoster@fortunejames.com
 
Posts: n/a

Default Re: MS-SQL Server and Math - 01-03-2008 , 11:05 PM



On Dec 30 2007, 4:45*pm, lyle <lyle.fairfi... (AT) gmail (DOT) com> wrote:
Quote:
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.
A shot in the dark:

http://msdn2.microsoft.com/en-us/library/bb508963.aspx

suggests, if I understand it correctly, that the vardecimal storage
format can store (integral) decimal type values almost as efficiently
as it can store integers. A Google search using 'SQL Server
vardecimal' turned up other interesting facts, such as which versions/
editions/SPlevels of SQL Server can make use of it.

James A. Fortune
CDMAPoster (AT) FortuneJames (DOT) com


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.