dbTalk Databases Forums  

How does your MOD() function work?

comp.databases comp.databases


Discuss How does your MOD() function work? in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #11  
Old   
Philipp Post
 
Posts: n/a

Default Re: How does your MOD() function work? - 06-09-2010 , 02:28 PM






Quote:
The MOD() function in various SQL products works differently. *I need
to update a table in SQL FOR SMARTIES. Mind helping me out? *Tell me
the name and release of your SQL, and the results of these function
calls.

-- SQL Server Express 2008 SP1 (10.0.2531)
-- No MOD available, but %

SELECT 12 % 5 = 2
SELECT -12 % 5 = -2
SELECT -12 % -5 = -2
SELECT 12 % -5 = 2
SELECT CAST(NULL AS INTEGER) % 5 = NULL
SELECT CAST(NULL AS INTEGER) % CAST(NULL AS INTEGER) = NULL
SELECT 12 % CAST(NULL AS INTEGER) = NULL
SELECT 12 % 0 = Error 8134 Division by Zero
SELECT -12 % 0 = Error 8134 Division by Zero
SELECT 0 % 5 = 0
SELECT 0 % -5 = 0
SELECT 0 % 0 = Error 8134 Division by Zero

-- IBM DB2 Express-C 9.7.0.441 LUW on Windows
VALUES MOD (12, 5) = 2
VALUES MOD (-12, 5) = -2
VALUES MOD (-12, -5) = -2
VALUES MOD (12, -5) = 2
VALUES MOD (CAST(NULL AS INTEGER), 5) = NULL
VALUES MOD (CAST(NULL AS INTEGER), CAST(NULL AS INTEGER)) = NULL
VALUES MOD (12, CAST(NULL AS INTEGER)) = NULL
VALUES MOD (12, 0) = Error SYSFUN:02 Division by Zero
VALUES MOD (-12, 0) = Error SYSFUN:02 Division by Zero
VALUES MOD (0, 5) = 0
VALUES MOD (0, -5) = 0
VALUES MOD (0, 0) = Error SYSFUN:02 Division by Zero

brgds

Philipp Post

Reply With Quote
  #12  
Old   
Jarl Hermansson
 
Posts: n/a

Default Re: How does your MOD() function work? - 06-10-2010 , 01:51 AM






--CELKO-- <jcelko212 (AT) earthlink (DOT) net> wrote in news:0ea81dbb-8093-41ce-b7a7-
a18c4f2f24db (AT) g19g2000yqc (DOT) googlegroups.com:

Quote:
The MOD() function in various SQL products works differently. I need
to update a table in SQL FOR SMARTIES. Mind helping me out? Tell me
the name and release of your SQL, and the results of these function
calls.
....

I originally found three different approaches to this problem. Answers
were 2, -2, NULL, error, 3, -3, 12 and -12. I just hope that things
are more consistent today.


Another case that may give different results is MOD(NULL,0). Mimer SQL
returns NULL.


/Jarl

Reply With Quote
  #13  
Old   
Dieter Noeth
 
Posts: n/a

Default Re: How does your MOD() function work? - 06-10-2010 , 03:46 AM



Forgot to add:
In Teradata MOD must be written as "12 MOD 5" instead of MOD(12,5)
And "NULL MOD 0" returns NULL.

Dieter

Dieter Noeth wrote:
Quote:
Teradata 12.00.03.07, same results for any other release:

MOD (12, 5) = 2

MOD(-12, 5) = -2

MOD (-12, -5) = -2

MOD (12, -5) = 2

MOD (NULL, 5) = NULL

MOD (NULL, NULL) = NULL

MOD (12, NULL) = NULL

MOD (12, 0) = Failure 2650 Numeric processor operand error.

MOD (-12, 0) = Failure 2650 Numeric processor operand error.

MOD (0, 5) = 0

MOD (0, -5) = 0

MOD (0, 0) = Failure 2650 Numeric processor operand error.

Reply With Quote
  #14  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: How does your MOD() function work? - 06-10-2010 , 08:15 AM



On 2010-06-10 07:51, Jarl Hermansson wrote:
Quote:
--CELKO-- <jcelko212 (AT) earthlink (DOT) net> wrote in news:0ea81dbb-8093-41ce-b7a7-
a18c4f2f24db (AT) g19g2000yqc (DOT) googlegroups.com:

The MOD() function in various SQL products works differently. I need
to update a table in SQL FOR SMARTIES. Mind helping me out? Tell me
the name and release of your SQL, and the results of these function
calls.
...

I originally found three different approaches to this problem. Answers
were 2, -2, NULL, error, 3, -3, 12 and -12. I just hope that things
are more consistent today.



Another case that may give different results is MOD(NULL,0). Mimer SQL
returns NULL.
db2 9.5 returnes null in this case as well


/Lennart

Reply With Quote
  #15  
Old   
John B. Matthews
 
Posts: n/a

Default Re: How does your MOD() function work? - 06-10-2010 , 11:03 AM



In article <huql0r$3bb$1 (AT) news (DOT) eternal-september.org>,
Lennart Jonsson <erik.lennart.jonsson (AT) gmail (DOT) com> wrote:

Quote:
On 2010-06-10 07:51, Jarl Hermansson wrote:
--CELKO-- <jcelko212 (AT) earthlink (DOT) net> wrote in news:0ea81dbb-8093-41ce-b7a7-
a18c4f2f24db (AT) g19g2000yqc (DOT) googlegroups.com:

The MOD() function in various SQL products works differently. I need
to update a table in SQL FOR SMARTIES. Mind helping me out? Tell me
the name and release of your SQL, and the results of these function
calls.
...

I originally found three different approaches to this problem. Answers
were 2, -2, NULL, error, 3, -3, 12 and -12. I just hope that things
are more consistent today.


Another case that may give different results is MOD(NULL,0). Mimer SQL
returns NULL.

db2 9.5 returnes null in this case as well
H2 Database 1.2.137 does likewise.

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

Reply With Quote
  #16  
Old   
--CELKO--
 
Posts: n/a

Default Re: How does your MOD() function work? - 06-10-2010 , 07:03 PM



Here is the ANSI/ISO definition for reference:

MOD(n, m) is the function that performs modulo or remainder
arithmetic. If either n or m is NULL, then the result is NULL. If m is
zero, then we get a division by zero exception. Otherwise, the result
is the unique non-negative exact numeric value r with scale zero such
that

1)r has the same sign as n.
2)the absolute value of r is less than the absolute value of m.
3)n = m * k + r for some exact numeric value k with scale zero.

Reply With Quote
  #17  
Old   
--CELKO--
 
Posts: n/a

Default Re: How does your MOD() function work? - 06-10-2010 , 07:07 PM



Quote:
returns:
a, b, c, d, e, f, g, h, i, j, k, l
2,-2,-2, 2, , , , , , 0, 0,
<<

Are the comma/empty strings to show NULLs, errors, or something else?

Reply With Quote
  #18  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: How does your MOD() function work? - 06-11-2010 , 01:50 AM



On 2010-06-11 01:03, --CELKO-- wrote:
[...]
Quote:
Otherwise, the result is the unique non-negative exact numeric value r with scale zero such
that

1)r has the same sign as n.
For negative n should r be positive or negative?

[...]

/Lennart

Reply With Quote
  #19  
Old   
Jasen Betts
 
Posts: n/a

Default Re: How does your MOD() function work? - 06-11-2010 , 06:39 AM



On 2010-06-11, Lennart Jonsson <erik.lennart.jonsson (AT) gmail (DOT) com> wrote:
Quote:
On 2010-06-11 01:03, --CELKO-- wrote:
[...]
Otherwise, the result is the unique non-negative exact numeric value r with scale zero such
that

1)r has the same sign as n.

For negative n should r be positive or negative?
from the above I think that version of the standard only allows for whole number n



--- news://freenews.netfront.net/ - complaints: news (AT) netfront (DOT) net ---

Reply With Quote
  #20  
Old   
David Kerber
 
Posts: n/a

Default Re: How does your MOD() function work? - 06-11-2010 , 08:33 AM



In article <b2465168-6750-4dc7-b983-
ee384eafa07f (AT) r27g2000yqb (DOT) googlegroups.com>, jcelko212 (AT) earthlink (DOT) net
says...
Quote:
returns:

a, b, c, d, e, f, g, h, i, j, k, l
2,-2,-2, 2, , , , , , 0, 0,


Are the comma/empty strings to show NULLs, errors, or something else?
Sorry; all are Nulls. They just didn't show as such when I did a
copy/paste from isql. No exceptions were returned.

Dave

Reply With Quote
Reply




Thread Tools
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 - 2012, Jelsoft Enterprises Ltd.