IS NULL not working in WHERE clause of SQL - sql

I have written a Stored procedure in which in given table column named xx can have 0,1 or null. When I give the below condition. SP is ignoring null and returning data only for 0 value.
WHERE (CAR_INSPECTION_NEW_TEST.NODAMAGEFLAG is null OR
CAR_INSPECTION_NEW_TEST.NODAMAGEFLAG = 0) AND
CAR_INSPECTION_NEW_TEST.ISSUBJECT_TODELIVER = 0
Can any one tell what is the problem?

By simple logic it will not return rows where NODAMAGEFLAG is null as long as ISSUBJECT_TODELIVER = 0 is not also valid (because of the AND).
So, check your data please.

Related

Update binary column based on integer column - Postgres

I have some legacy data with a binary column, fish_otolith, that is not accurate. It should indicate based on separate column named fish_age. When an age is present in the fish_age column the binary column, fish_otolith, should indicate and when a null value is present in the fish_age column the binary column, fish_otolith, should not indicate.
fish_otolith
fish_age
1
10
1
2
0
Null
1
Null
1
Null
I am struggling with writing the correct update sql statement of "if fish_age is not Null then fish_otolith is equal to 1, else if fish is null then fish_otolith is equal to 0
If your fish_otolith is a boolean, then try this:
update ttable
set fish_otolith = fish_age is not null;
The expression fish_age is not null returns a boolean value which can be cast as an integer with the result false -> 0 and true -> 1, which is the results you are looking for. So:
update fishes
set fish_otolith = (fish_age is not null)::integer;
This (or any other update) however contains a potential fatal flaw: fish_otolith can be updated independent of fish_age and can contain any valid integer not just 0 or 1. This con be overcome with a slight design change. Define fish_otolith as a generated always ... derived from fish_age. (see demo here - for both implementations).
Note: Either setting by #MikeOrganek would also work.

How to modify stored procedure to give null instead of 0

How to modify a stored procedure to return null instead of 0 ?
Modification request to a stored procedure used for a report. Asked to display blank in the report. Meaning need to return null instead of 0 (zero).
Current code:
Approach = ISNULL(Approach, 0) ,
Possible new code:
Approach = ISNULL(Approach, '') ,
In you sql code add this line
CASE WHEN Approach = 0 THEN NULL END AS Approach
so in your stored procedure when Approach will come as 0 then it will be replaced with NULL
Went about it in SSDT/SSRS
=IIF(Sum(Fields!"Field".value)=0,"",Sum(Fields!"Field".value))

Matching BIT to DATETIME in CASE statement

I'm attempting to create a T-SQL case statement to filter a query based on whether a field is NULL or if it contains a value. It would be simple if you could assign NULL or NOT NULL as the result of a case but that doesn't appear possible.
Here's the psuedocode:
WHERE DateColumn = CASE #BitInput
WHEN 0 THEN (all null dates)
WHEN 1 THEN (any non-null date)
WHEN NULL THEN (return all rows)
From my understanding, the WHEN 0 condition can be achieved by not providing a WHEN condition at all (to return a NULL value).
The WHEN 1 condition seems like it could use a wildcard character but I'm getting an error regarding type conversion. Assigning the column to itself fixes this.
I have no idea what to do for the WHEN NULL condition. My internal logic seems to think assigning the column to itself should solve this but it does not as stated above.
I have recreated this using dynamic SQL but for various reasons I'd prefer to have it created in the native code.
I'd appreciate any input. Thanks.
The CASE expression (as OMG Ponies said) is mixing and matching datatypes (as you spotted), in addition you can not compare to NULL using = or WHEN.
WHERE
(#BitInput = 0 AND DateColumn IS NULL)
OR
(#BitInput = 1 AND DateColumn IS NOT NULL)
OR
#BitInput IS NULL
You could probably write it using CASE but what you want is an OR really.
You can also use IF..ELSE or UNION ALL to separate the 3 cases

Addition with NULL values

In a stored procedure (Oracle in my case), I want to add some values to an existing record. Problem is that both the existing value and the value to be added can be null. I only want the result to be NULL when both operands are null. If only one of them is null, I want the result to be the other operand. If both are non-null, I want the result to be "normal" addition.
Here's what I am using so far:
SELECT column INTO anz_old FROM aTable Where <someKeyCondition>;
IF anz_old IS NULL
THEN
anz_new := panzahl;
ELSE
anz_new := anz_new + NVL (panzahl, 0);
END IF;
UPATE aTabel set column = anz_new Where <someKeyCondition>;
Is there a more elegant way (pereferably completely in SQL, i.e. just in an update statement short of a long CASE-Statement with basically the same logic as the above code)?
If you want to add a and b and either may be null, you could use coalesce, which returns the first non-null parameter you pass it:
coalesce(a+b, a, b)
So in this case, if neither parameter is null, it will return the sum. If only b is null, it will skip a+b and return a. If a is null, it will skip a+b and a and return b, which will only be null if they are both null.
If you want the answer to be 0 rather than null if both a and b are null, you can pass 0 as the last parameter:
coalesce(a+b, a, b, 0)
Do consider #erwins answer - null might not be the right thing to be using.
I accomplished it this way:
coalesce("Column1",0.00) + coalesce("Column2",0.00)
I'm working with front end high level execs.... They don't understand why NULL and 0 aren't handled the same way.
In my case it works, just replacing NULLs with 0.00... may not in all though :)
You can also use ISNULL, so if you have 3 values
isnull(val1,0)+isnull(val2,0)+isnull(val3,0)
which ever column will have a NULL will use a 0, otherwise its original value.
In SQL, Null is supposed to be a state that says "I don't know".
If you don't know how much b is, then you also do not know how much a+b is, and it is misleading to pretend that a+b=a in that case.
In SQL terms, when adding numbers, a result of NULL means there were no non-null numbers added.
This suggests that a sensible answer in SQL terms would be
CASE WHEN A IS NULL AND B IS NULL THEN NULL ELSE ISNULL(A, 0) + ISNULL(B, 0) END

SQLServer Get Results Where Value Is Null

I have an SQL server database that I am querying and I only want to get the information when a specific row is null. I used a where statement such as:
WHERE database.foobar = NULL
and it does not return anything. However, I know that there is at least one result because I created an instance in the database where 'foobar' is equal to null. If I take out the where statement it shows data so I know it is not the rest of the query.
Can anyone help me out?
Correct syntax is WHERE database.foobar IS NULL. See http://msdn.microsoft.com/en-us/library/ms188795.aspx for more info
Comparison to NULL will be false every time. You want to use IS NULL instead.
x = NULL -- always false
x <> NULL -- always false
x IS NULL -- these do what you want
x IS NOT NULL
Read Testing for Null Values, you need IS NULL not = NULL
Is it an SQL Server database?
If so, use IS NULL instead of making the comparison (MSDN).