473,385 Members | 1,311 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

NZ function and Max wierdness with JET SQL

Using the following SQL can the results be explained?

Using A97 (with the SP2 for Jet 3.5) or A2002

CREATE TABLE Test
(PK Number CONSTRAINT PK_TEST PRIMARY KEY, ParentID Number,
Child Number);

INSERT INTO Test VALUES(1,1,1);
INSERT INTO Test VALUES(2,1,2);
INSERT INTO Test VALUES(3,1,10);
'with an NZ wrapper
SELECT Max(nz([Child],0)) AS MaxChild
FROM Test
WHERE Test.ParentID = 1;

'without NZ Wrapper
SELECT Max([Child]) AS MaxChild
FROM Test
WHERE Test.ParentID = 1;

- with an NZ wrapper if the max is >=10, the value 2 will be returned by
the query engine.
The actual max will be returned without the nz function wrapping the
return value ie 10.

Now

UPDATE TEST SET Child = 3 WHERE PK = 3

- If the max value is <10 both SELECTs will return the same result ie 3.

Using SQL Server 2000 and

--with an ISNULL wrapper
SELECT Max(ISNULL([Child],0)) AS MaxChild
FROM Test
WHERE Test.ParentID = 1; both with and with ISNULL return the same result
ie 10, as I would expect.

What am I missing here? Can anyone explain the results sets and why nz
would cause a difference only if the max is >=10?

Peter
Nov 12 '05 #1
2 6608
The problem occurs because Nz() returns a variant, and JET misunderstands
this to be a string. The value "2" is returned because the string "2" is
greater than the string "10".

You can verify this is the problem with:
SELECT TypeName(Max(nz([Child],0))) AS MaxChild
FROM Test WHERE ParentID = 1;
The query returns the word "String".
If you copy your query into SQL View of the query design, and view the
result, you will see the 2 align left, indicating that Access interprets it
as a string value.

Your example is a very timely reminder that *any* calculated field (other
than string) should be explicitly typecast, i.e.:
SELECT CDbl(Nz(Max(Child),0)) AS MaxOfChild
FROM Test WHERE ParentID = 1;
For dates, CVDate() is very useful, as it also handles Nulls and may avoid
the need to use Nz().

(Note: the query I've suggested will return a negative value as the max,
whereas the one you posted may not: since you used Nz() before choosing the
max, the zero would be greater than all negative values.)

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html

"Pink Panther" <pi*************@mail.com> wrote in message
news:3f********@news.iprimus.com.au...
Using the following SQL can the results be explained?

Using A97 (with the SP2 for Jet 3.5) or A2002

CREATE TABLE Test
(PK Number CONSTRAINT PK_TEST PRIMARY KEY, ParentID Number,
Child Number);

INSERT INTO Test VALUES(1,1,1);
INSERT INTO Test VALUES(2,1,2);
INSERT INTO Test VALUES(3,1,10);
'with an NZ wrapper
SELECT Max(nz([Child],0)) AS MaxChild
FROM Test
WHERE Test.ParentID = 1;

'without NZ Wrapper
SELECT Max([Child]) AS MaxChild
FROM Test
WHERE Test.ParentID = 1;

- with an NZ wrapper if the max is >=10, the value 2 will be returned by
the query engine.
The actual max will be returned without the nz function wrapping the
return value ie 10.

Now

UPDATE TEST SET Child = 3 WHERE PK = 3

- If the max value is <10 both SELECTs will return the same result ie 3.

Using SQL Server 2000 and

--with an ISNULL wrapper
SELECT Max(ISNULL([Child],0)) AS MaxChild
FROM Test
WHERE Test.ParentID = 1; both with and with ISNULL return the same result
ie 10, as I would expect.

What am I missing here? Can anyone explain the results sets and why nz
would cause a difference only if the max is >=10?

Peter

Nov 12 '05 #2
Makes perfect sense (now).

Thanks Allen.

"Allen Browne" <ab***************@bigpond.net.au> wrote in
news:w%*******************@news-server.bigpond.net.au:
The problem occurs because Nz() returns a variant, and JET
misunderstands this to be a string. The value "2" is returned because
the string "2" is greater than the string "10".

Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
13
by: Tek Boy | last post by:
I've been experiencing some (reproducable) wierdness when I try to generate some very basic HTML using ASP. Check out the following (basic) ASP code: ===========================================...
5
by: phil_gg04 | last post by:
Dear Javascript Experts, Opera seems to have different ideas about the visibility of Javascript functions than other browsers. For example, if I have this code: if (1==2) { function...
0
by: amber | last post by:
Hello, I'm having some wierdness with a report I've created in VB.NET (with Crysal Reports). The report is called repCPDocSubmissionSummary.vb I created it a while ago, and have been using it...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
2
by: sushil | last post by:
+1 #include<stdio.h> +2 #include <stdlib.h> +3 typedef struct +4 { +5 unsigned int PID; +6 unsigned int CID; +7 } T_ID; +8 +9 typedef unsigned int (*T_HANDLER)(void); +10
5
by: Eduardo Olivarez | last post by:
The following code does not work correctly on my machine. Either one of the scanf()'s alone work perfectly. However, when they are combined, the second scanf() call just reads what the first one...
8
by: Olov Johansson | last post by:
I just found out that JavaScript 1.5 (I tested this with Firefox 1.0.7 and Konqueror 3.5) has support not only for standard function definitions, function expressions (lambdas) and Function...
9
by: Bobby Edward | last post by:
Are there any add-ons to Visual Studio 2008 that will help me troubleshoot CSS wierdness? Like showing the padding/margin with difference colors, etc...? I'm trying to figure out some wierd...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.