473,657 Members | 2,504 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6629
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(Chi ld),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.c om.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%******** ***********@new s-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
14928
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) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
13
1665
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: =========================================== <% Option Explicit Const STRING_1 = "/admin/UploadProgress2.asp" Const STRING_A = "AXFFile" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html>
5
2823
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 invisible() { alert("invisible() called"); } }
0
1340
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 successfully for months. Today I went in and did some edits, saved and closed it. Then, when I tried to build my project, I kept getting an error about ambiguous name (repCPDocSubmissionSummary). I looked in the folder, and saw there was a...
2
7671
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 like mine to run immediately after it. So in the code below, what JS would i need to add to my "myfile.inc" page so that I could guarantee this behavior? <!-- main page --> <html> <head> <script type="text/javascript">
2
12680
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
3885
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 received as imput, without taking any keyboard input and then the process terminates prematurely. I looked through the glibc documentation trying to figure out what was causing this to no avail, so I started messing around with the code. Strangely...
8
5093
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 constructors (these three I knew about), but also conditional function definitions, as described in http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Defining_Functions ]. An example: function fun() {
9
1322
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 floating and spacing issues? Discrepencies between FF and IE...
0
8421
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8325
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8844
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8742
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8518
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7354
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
1971
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.