IsNotNull Access function - ms-access

I am trying to set a function that retur value if two conditions are satisfied
I have four textboxes:
PCA
Text151
Text152
Text153
I made Text153 to return -- if Text151 & Text152 are null, and to return Abs if Text151 is null and Text152 is not null, else return [PCA].
=IIf(IsNull([Text151]) and IsNull([Text152]), "--",IIf(IsNull([Text151]) and IsNotNull([Text152]),"Abs","[PCA]))
The function shows only -- but the others expected returns could not be shown.

When testing for null in a query, always use the more efficient is null and is not null SQL keywords, rather than the less efficient isNull VBA function.
Hence, your expression may become:
=iif([text151] is null and [text152] is null,"--",iif([text151] is null and [text152] is not null,"Abs",[PCA]))
Note that this could also be written:
=iif([text151] is null,iif([text152] is null,"--","Abs"),[PCA])
As you only need to test whether the textbox is null once.

Related

Blank input Parameters in a WHERE condition of a SELECT statement

I have a report with 10 input parameters wherein only two are obligatory, the rest are optional. The problem is, as far as I know, if you use an input parameter that is blank in a WHERE condition of a SELECT statement no records will be selected from a table, so how to fix that ?
lets try something simple:
SELECT * FROM contrat WHERE article = 'sumsung' AND price = null
the user entered an article and did not enter a price
so the query will return nothing because price is null even when we have a contract with a sumsung article
If by blank you mean null or an empty string, you must construct for each of the optional parameters a condition like:
column_name = :optional_param OR :optional_param IS NULL
or:
column_name = :optional_param OR :optional_param = ''
So, your WHERE clause should look like:
WHERE <conditions for the non null parameters>
AND (column_name1 = :optional_param1 OR :optional_param1 IS NULL)
AND (column_name2 = :optional_param2 OR :optional_param2 IS NULL)
AND .......

How do I add conditions in a WHERE SQL clause or return all results if filters are empty

I have an SQL statement with a WHERE clause where I am adding additional filters if a variable passed into the stored procedure IN parameter is a certain value.
The IN parameter is called filterID and can be empty if I need all records, or have the value SEN,EAL or MAGT if I want to only select the records which have IDs in those columns from the LEFT JOINED tables.
This is the extract from the SQL query to show the joining of the tables and the additional filter.
LEFT JOIN tbl_sen ON tbl_pupil.pupilID = tbl_sen.pupilID
LEFT JOIN tbl_eal ON tbl_pupil.pupilID = tbl_eal.pupilID
LEFT JOIN tbl_magt ON tbl_pupil.pupilID = tbl_magt.pupilID
WHERE
tbl_test.schoolID=schoolID
AND (
(filterID='SEN' AND tbl_sen.senID IS NOT NULL) OR
(filterID='EAL' AND tbl_eal.ealID IS NOT NULL) OR
(filterID='MAGT' AND tbl_magt.magtID IS NOT NULL))
AND tbl_test.import=1;
WHen I execute the stored procedure with the values, it shows the correct results. However, if I don't want to add the filter and pass filterID as empty/NULL, it returns 0 results when I need it to return ALL the results.
Can anyone spot what I'm doing wrong?
...
AND CASE filterID WHEN 'SEN' THEN tbl_sen.senID IS NOT NULL
WHEN 'EAL' THEN tbl_eal.ealID IS NOT NULL
WHEN 'MAGT' THEN tbl_magt.magtID IS NOT NULL
ELSE filterID IS NULL END
AND ...
As I understand the filterID is local variable (SP parameter) so there is no problem to use it twice (rather than bound parameter of the query - in this case you may use user-defined intermediate variable).
Simplified DEMO
AND (
(filterID='SEN' AND tbl_sen.senID IS NOT NULL) OR
(filterID='EAL' AND tbl_eal.ealID IS NOT NULL) OR
(filterID='MAGT' AND tbl_magt.magtID IS NOT NULL) OR
(filterID IS NULL)
)

mysql - detecting identical rows when theres no primary key

I'm dealing with a situation at work where someone set up some tables without a primary key (arghhh). now I'm trying to compare dev data to prod data and the best I can tell is that theres a slight difference in the number of rows.
I figured the best way I could compare data is to do a join on every column, but I'm getting unexpected results.
to test this out I just joined the same table to itself. This table has 1309 rows.. but when I join on every column i get 1014. Thats less rows. If anything i'd expect to get more. What gives?
select *
from `default_tire_classifications` tc1
join `default_tire_classifications` tc2
on tc1.`marketing_tread_name` = tc2.`marketing_tread_name`
AND tc1.`size` = tc2.`size`
AND tc1.product_category = tc2.product_category
AND tc1.application = tc2.application
AND tc1.vehicle_type = tc2.vehicle_type
AND tc1.oem_part = tc2.oem_part
AND tc1.position = tc2.position
AND tc1.size = tc2.size
AND tc1.sect_wdth = tc2.sect_wdth
AND tc1.aspect_ratio = tc2.aspect_ratio
AND tc1.rim_size = tc2.rim_size
AND tc1.speed_rating = tc2.speed_rating
AND tc1.load_index = tc2.load_index
I suspect some of the columns contain NULL values. An equality comparison to a NULL value yields NULL. (SQL tri-valued Boolean logic.)
To do a comparison that yields TRUE when both sides are NULL, you could do something like this
( tc1.col = tc2.col OR ( tc1.col IS NULL AND tc2.col IS NULL ) )
MySQL also provides a non-standard "null-safe" equality comparison operator <=> (spaceship) that does the same thing.
tc1.col <=> tc2.col
returns either TRUE or FALSE, and will return TRUE when the values on both sides are NULL.
So, replacing the = (equality comparison) operator with <=> operator should resolve the problem with comparing NULL values.
(This isn't to say that NULL values is a problem, or is the only problem.)
If your fields contains NULL, you will lost them. Because NULL = NULL is not true.

Conditional Split fails if value is NULL in SSIS

I am passing result of FULL Outer join to Conditional Split and Filtering Records on the basis of following rules . Basically both tables has same schema and Primarykey values are same.
a. If Primary key of Source is NULL
b. If Primary Key of Destination is NULL
c. If Source and Destination key matches.
It works fine for (a) and (b) but fails for (c)
Source.Id == Destination.Id
and throws exception that condition evaluated as NULL where Boolean was expected. How i can make this work?
Conditional Split gets input from Merge Join and it's a FULL OUTER JOIN as i need FULL OUTER join results here
Your third condition should start with a ISNULL check again before you compare your values. Like the following:
!ISNULL(Source.Id) && !ISNULL(Destination.Id) && Source.Id == Destination.Id
You need to handle every column that can be NULL in your condition.
Since you are comparing Id's, another option would be:
(ISNULL(Source.Id) ? 0 : Source.Id) == (ISNULL(Destination.Id) ? 0 : Destination.Id)
If comparing strings, you can replace the zeroes with blank spaces.
Alternatively you can use the following syntax:
REPLACENULL(Source.Id,0) == REPLACENULL(Destination.Id,0)

MySQL Stored Pocedures skip WHERE condition if parameter is null

I'm writing stored procedure that should search for records base on few procedure arguments.
The problem is that not always all arguments have to be passed, sometimes they may be set to NULL.
Is there a way to write sth that would work like that?
CREATE PROCEDURE testProc(IN p_idWorker INTEGER, IN p_idEffect INTEGER)
BEGIN
SELECT
*
FROM
CallHistory
WHERE
idWorker = IFNULL(p_idWorker, ANYTHING)
AND
idEffect = IFNULL(p_idEffect, ANYTHING);
END$$
Like so:
...
WHERE (p_idWorker IS NULL OR idWorkder = p_idWorker)
AND (p_idEffect IS NULL OR idEffect = p_idEffect);
Or, like the way you did, but instead of Anything use the column name instead like so:
...
WHERE
idWorker = IFNULL(p_idWorker, idWorker )
AND
idEffect = IFNULL(p_idEffect, idEffect );
You can use for example:
idWorker = IFNULL(p_idWorker, idWorker)
IF p_idWorker is null then this condition is always TRUE for all rows. If not then it is true only if idWorker = p_idWorker
First of all thank you Mahmoud and valex for your time but both answers are not totally good. They will not work if for example the field idWorker is nullable - it will not see the rows where field idWorker IS NULL.
The ultimate solution to this looks weird but it works:
...
WHERE
idWorker = IFNULL(p_idWorker, idWorker)
AND
IFNULL(ch.idProjTime, -1) = IFNULL(p_idProjTime, IFNULL(ch.idProjTime, -1))
Now it'll see NULL fields too.
If it is bad idea to sth like that (I can see probable performance impact - 3 times a row it does the IFNULL condition) - please correct me.