Wrong number of arguments with SQL ISNULL() on Access DB - sql

I have this query in VB application on Access DB:
SELECT DISTINCT Specialization, MAX(a.faultZone) AS faultZone, ISNULL(a.faultCount, 0) AS NoOfFaults FROM Technicians AS t
LEFT JOIN
(
SELECT DISTINCT Faults.[Type] AS faultType, MAX(Faults.[Zone]) AS faultZone, COUNT(Faults.[Type]) AS faultCount
FROM Faults "
WHERE Faults.[Zone] = 8 " ' this value will be from variable
GROUP BY Faults.[Type] "
) AS a
ON (t.Specialization = a.faultType)
WHERE t.specialization <> 'None' "
GROUP BY a.faultCount, t.Specialization
It gives following problem that I can't solve...
"Wrong number of arguments used with function in query expression
'ISNULL(a.faultCount, 0'."
What I want to achieve is simply set value of NoOFFaults to zero, which would mean there are no faults in particular Zone.
Thank You

Just to add my two cents, and while I like the simple syntax of Nz(), if you seek trouble free performance, both IsNull() and NZ() should be avoided in favor of Is Null:
IIF(a.faultCount Is Null, 0, a.faultCount).
See the excellent explanation here: http://allenbrowne.com/QueryPerfIssue.html
Also, if your tables are in SQL Server or Oracle, using Nz() will force more of the query to be executed locally, with a HUGE performance impact.

Microsoft Access' version of IsNull is different than most SQL versions; it simply returns TRUE if the value is NULL, and FALSE if it isn't.
You need to basically build your own using IIF():
IIF(ISNULL(a.faultCount), 0, a.faultCount)

I think that you are looking for the nz function
Nz(a.faultCount, 0)
will return 0 if the value is null

Related

Oracle: True value in WHERE

I am building a SQL query inside a function. The functions parameters are my criterias for the WHERE clause. Parameters can be null too.
foo (1,2,3) => SELECT x FROM y WHERE a=1 AND b=2 AND c=3;
foo (null, 2, null) => SELECT x FROM y WHERE b=2;
My approach to do that in code is to add a very first alltime true in the WHERE-clause (e.g. 1=1 or NULL is NULL or 2 > 1)
So I do not need to handle the problem that the 1st WHERE condition is after a "WHERE" and all others are after a "AND".
String sql="SELECT x FROM y WHERE 1=1";
if (a!=null)
{
sql += " AND a="+a;
}
Is there a better term than 1=1 or my other samples to EXPLICITLY have a always true value? TRUE and FALSE is not working.
Oracle does not support a boolean type in SQL. It exists in PL/SQL, but can't be used in a query. The easiest thing to do is 1=1 for true and 0=1 for false. I'm not sure if the optimizer will optimize these out or not, but I suspect the performance impact is negligible. I've used this where the number of predicates is unknown until runtime.
I think this construct is superior to anything using nvl, coalesce, decode, or case, because those bypass normal indexes and I've had them lead to complicated and slow execution plans.
So, yes I'd do something like you have (not sure what language you're building this query in; this is not valid PL/SQL. Also you should use bind variables but that's another story):
sql = "SELECT x FROM y WHERE 1=1";
if (a != null)
{
sql += " AND a=" + a;
}
if (b != null)
{
sql += " AND b=" + b;
}
... etc.
I think this is better than Gordon Linoff's suggestion, because otherwise this would be more complicated because you'd have to have another clause for each predicate checking if there was a previous predicate, i.e. whether you needed to include the AND or not. It just makes the code more verbose to avoid a single, trivial clause in the query.
I've never really understood the approach of mandating a where clause even when there are no conditions. If you are constructing the logic, then combine the conditions. If the resulting string is empty, then leave out the where clause entirely.
Many application languages have the equivalent of concat_ws() -- string concatenation with a separator (join in Python, for instance). So leaving out the where clause does not even result in code that is much more complicated.
As for your question, Oracle doesn't have boolean values, so 1=1 is probably the most common approach.
use NVL so you can set a default value. Look up NVL or NVL2. Either might work for you.
check out: http://www.dba-oracle.com/t_nvl_vs_nvl2.htm
In sql the standard way to "default null" is to use coalesce. So lets say your input is #p1, #p2, and #p3
Then
select *
from table
where a = coalesce(#p1,a) and
b = coalesce(#p2,b) and
c = coalesce(#p3,c)
Thus if (as an example) #p1 is null then it will compare a = a (which is always true). If #p1 is not null then it will filter on a equals that value.
In this way null becomes a "wildcard" for all records.
you could also use decode to get the results you need since it is oracle
Select
decode(a, 1, x,null),
decode(b, 2, x,null),
decode(c, 3, x,null)
from y
What about using OR inside of the parentheses while ANDs exist at outside of them
SELECT x
FROM y
WHERE ( a=:prm1 OR :prm1 is null)
AND ( b=:prm2 OR :prm2 is null)
AND ( c=:prm3 OR :prm3 is null);

access statement convert to Sql

how can I convert to T-Sql this one?
IIf([ESSValue]<>0,Int([ESSValue]*100),"")
I think the following pretty much does what you want:
select coalesce(cast(EssValue * 100 as int), 0)
Here is the thinking. The comparison to zero is unimportant, because 0 times any value is going to be zero. The iif() returns an integer (I think) because the "then" argument is an integer; the empty string gets converted to zero.
I'm not 100% certain about the last statements with regard to MS Access, but that is how iif() works in SQL Server.
I should add. Although I don't approve of iif() for conditional expressions (because case is the standard and more powerful), SQL Server does support it. So you could write:
IIf([ESSValue]<>0, cast([ESSValue]*100 as int), '')
Note: As I mentioned earlier, the '' will be converted to 0.
CASE WHEN ESSValue <> 0
THEN CAST(ESSValue * 100 AS INT)
ELSE NULL
END as fieldname
For case expression the default is NULL if doesn't meet any condition, so you dont really need the ELSE condition

Changing query from access SQL to SQL server

I created this query in Microsoft access and I am trying to run it in SQL Server 2012 but I am getting several errors I have tried a few different ways to fix it but I still cant get it working
SELECT Choose(Iif(IsNull([TaxCodeChangeTypeID]),0,
[TaxCodeChangeTypeID])+1,
'UNKNOWN',
'UPLIFT',
'PAPERP6',
'PAPERP9',
'P453',
'P46',
'HMRC',
'DPSP6',
'DPSP9',
'P46(Pen)',
'P46(Expat)') AS [Tax Code Change Type]
FROM EeBals
As mentioned in comments, ISNULL in Access takes a single parameter and returns a boolean value depending on whether the parameter passed is null, in SQL Server ISNULL takes two arguments, the first is the value to check, and the second is the value to return if the value is null, so your line:
Iif(IsNull([TaxCodeChangeTypeID]),0, [TaxCodeChangeTypeID])+1
Needs to be replaced with
ISNULL([TaxCodeChangeTypeID], 0) + 1
To acheive the same thing. Making your full query:
SELECT CHOOSE(ISNULL([TaxCodeChangeTypeID], 0) + 1,
'UNKNOWN',
'UPLIFT',
'PAPERP6',
'PAPERP9',
'P453',
'P46',
'HMRC',
'DPSP6',
'DPSP9',
'P46(Pen)',
'P46(Expat)') AS [Tax Code Change Type]
FROM EeBals
Not being able to test it, but as it seems ISNULL should be the only issue here.
In MS Access, ISNULL returns true if the checked value is in fact null, but in SQL Server it works like a COALESCE (but only taking 2 instead of unlimited parameters) returning the second value if the first was null.
CHOOSE and Iif should work similarily to MS Access in SQL Server.
I would simply replace IsNull([TaxCodeChangeTypeID]) with [TaxCodeChangeTypeID] IS NULL
SELECT Choose
(
Iif
(
[TaxCodeChangeTypeID] IS NULL,
0,
[TaxCodeChangeTypeID]
)+1,
'UNKNOWN','UPLIFT','PAPERP6','PAPERP9','P453','P46','HMRC','DPSP6','DPSP9','P46(Pen)','P46(Expat)'
) AS [Tax Code Change Type]
FROM EeBals
Note: this only applies to SQL Server since the version 2012, before that neither IIF nor CHOOSE were present in either standard SQL Server or SQL Server Express
PS: of course the whole IIf(...)+1 can now be avoided and be a little more beautified as GarethD also mentioned in his post by writing ISNULL([TaxCodeChangeTypeID], 0) + 1 instead.

What applications are there for NULLIF()?

I just had a trivial but genuine use for NULLIF(), for the first time in my career in SQL. Is it a widely used tool I've just ignored, or a nearly-forgotten quirk of SQL? It's present in all major database implementations.
If anyone needs a refresher, NULLIF(A, B) returns the first value, unless it's equal to the second in which case it returns NULL. It is equivalent to this CASE statement:
CASE WHEN A <> B OR B IS NULL THEN A END
or, in C-style syntax:
A == B || A == null ? null : A
So far the only non-trivial example I've found is to exclude a specific value from an aggregate function:
SELECT COUNT(NULLIF(Comment, 'Downvoted'))
This has the limitation of only allowing one to skip a single value; a CASE, while more verbose, would let you use an expression.
For the record, the use I found was to suppress the value of a "most recent change" column if it was equal to the first change:
SELECT Record, FirstChange, NULLIF(LatestChange, FirstChange) AS LatestChange
This was useful only in that it reduced visual clutter for human consumers.
I rather think that
NULLIF(A, B)
is syntactic sugar for
CASE WHEN A = B THEN NULL ELSE A END
But you are correct: it is mere syntactic sugar to aid the human reader.
I often use it where I need to avoid the Division by Zero exception:
SELECT
COALESCE(Expression1 / NULLIF(Expression2, 0), 0) AS Result
FROM …
Three years later, I found a material use for NULLIF: using NULLIF(Field, '') translates empty strings into NULL, for equivalence with Oracle's peculiar idea about what "NULL" represents.
NULLIF is handy when you're working with legacy data that contains a mixture of null values and empty strings.
Example:
SELECT(COALESCE(NULLIF(firstColumn, ''), secondColumn) FROM table WHERE this = that
SUM and COUNT have the behavior of turning nulls into zeros. I could see NULLIF being handy when you want to undo that behavior. If fact this came up in a recent answer I provided. If I had remembered NULLIF I probably would have written the following
SELECT student,
NULLIF(coursecount,0) as courseCount
FROM (SELECT cs.student,
COUNT(os.course) coursecount
FROM #CURRENTSCHOOL cs
LEFT JOIN #OTHERSCHOOLS os
ON cs.student = os.student
AND cs.school <> os.school
GROUP BY cs.student) t

SQL DB2 null calculation causing problems

I have the following SQL:
Select dmvndn "Vendor Number", IFNULL(sum(dmsls) / sum(dmprc), 0) "Calculation"
From MyFile
Group By dmvndn
However, when i run this, i am still getting null records in my "Calculation" field.
I have also tried the COALESCE function, which returns the same results. I get some records as 0, and some records are blank (or, null).
Both fields are of type P, which i am told is packed numeric.
any ideas or suggestions?
Edit 1
It seems that the problem is not with either of these fields being NULL, it is that one or both fields are 0. And when i divide by zero, i get the empty / blank result.
Try
Sum(IFNULL(dmsls,0)) / Sum(IFNULL(dmprc,0))
A trick of this kind helps me in MS SQL Server:
Select
dmvndn "Vendor Number",
IFNULL(sum(dmsls) / NULLIF(sum(dmprc), 0), 0) "Calculation"
From MyFile
Group By dmvndn
I wonder if it can't help you in DB2.
UPDATE: an explanation.
Basically, it replaces the divisor with NULL if it's 0. And you may probably know that when at least one of the operands is NULL, the result of the operation becomes NULL as well.
To account for the result being NULL you already had your IFNULL on the result. It didn't make much difference then, because none of the operands was likely to be NULL. However, now using IFNULL makes perfect sense.