Case When Variable Is null then is null - oracle

In my sql query I'm having this where condition:
WHERE
parentID = prj.parent_id
and mandator_fk = mandator_id
The variable parentID is null as default.
If this variable is null the WHERE condition should be parentID is null and not like in the query above (parentID = prj.parent_id). If this variable is filled the WHERE condition should be parentID = prj.parent_id.
How can I perform this with only one sql query?
EDIT: its inside a function, thats why I would have only one sql query for this.
This is not working I know that, but this is like what I need:
WHERE
CASE
WHEN parentID is null THEN parentID is null
ELSE
parentID = prj.parent_ID
END
and mandator_fk = mandator_id
TIA
frgtv10

Seems you want to join on nulls, so you'll need to convert them first. Try this:
where
NVL(parentId, 0) = NVL(prj.parent_id, 0)
and ...

I'm not sure if NVL() function is surgable, so here is another way to write the condition:
WHERE
( parentID = prj.parent_id
OR parentID IS NULL AND prj.parent_id IS NULL
)
AND mandator_fk = mandator_id

Related

Oracle PL/SQL How to Get All Rows When the Date Parameter is Null?

This is where statement of my query:
WHERE date_column LIKE
(CASE
WHEN :date_parameter IS NOT NULL
THEN
:date_parameter
ELSE '%%'
END)
...
If the parameter is null, I want to get all rows. How can I do this?
Try
WHERE (:date_parameter is null
OR date_column = :date_parameter)
I used to write
WHERE date_column = nvl(:date_parameter,date_column)
But I think people find it confusing to read.
This should be work:
WHERE (date_column = :date_parameter AND :date_parameter IS NOT NULL) OR :date_parameter IS NULL

Add a Null column to a linq query

I want to do a LINQ query and add in a column that is null. Something like this:
var query = (from all in data
select new
{
all.Column1,
all.Column2,
all.Column3,
newColumn = null
})
However, this gives me the error "cannot assign null to anonymous type property".
Add the null with appropriate cast depending on the type of newColumn such as e.g. newColumn = (string) null;

using raw query in where methods makes error in query generation

I have an eloquent query as
Role::where("(company_id = Auth::user()->company_id or name = 'admin')and id in(2,3)")->pluck('name');
According to my eloquent the sql should be as
select `name` from `roles` where ( company_id = 1 or name = admin ) and id IN (2, 3) and `roles`.`deleted_at` is null
But it executes as
select `name` from `roles` where ( company_id = 1 or name = admin ) and id IN (2, 3) is null and `roles`.`deleted_at` is null
Can anyone help me concluding why extra is null condition is applied in the query?
Note: I am using soft deletes
You should use whereRaw instead of where
Role::whereRaw("(company_id = Auth::user()->company_id or name = 'admin')and id in(2,3)")->pluck('name');

Constraint check if row and other row not null on same time

I have a school 'project' to work on, which has some tables and one table needs to have a constraint which is not working out for me.
There are some tables like QUESTION, ANSWER and REACTION.
A reaction belongs with or a question or a answer but not both on the same time.
There by I have 2 rows:
question_id NUMBER,
answer_id NUMBER,
Both not null because the cant by null, but not on the same time.
I already made a constraint but isn't working..
/* CHECK if reaction belongs to an question or a answer NOT WORKING YET*/
CONSTRAINT CHECK_question_or_answer CHECK((answer_id != NULL AND question_id = NULL) OR (answer_id = NULL OR question_id != NULL))
Already tested the constraint and I can insert a value without a question_id or answer_id.
I hope it's a bit clear, if not, I am happy yo try explain myself better.
(still newby on SQL)
Thanks.
Your constraint:
CONSTRAINT CHECK_question_or_answer CHECK((answer_id != NULL AND profile_id = NULL) OR (answer_id = NULL OR profile_id != NULL))
Is always FALSE.
You need to use IS NULL or IS NOT NULL like:
CONSTRAINT CHECK_question_or_answer CHECK((answer_id IS NOT NULL AND profile_id IS NULL) OR (answer_id IS NULL OR profile_id IS NOT NULL))
This is because comparison operators != , = , > , <, combined with NULL produce NULL and are treated as false.
Demo:
SELECT 1
FROM dual
WHERE 1 IS NOT NULL;
SELECT 1
FROM dual
WHERE 1 != NULL;
From doc:
NULL values represent missing or unknown data. NULL values are used as
placeholders or as the default entry in columns to indicate that no
actual data is present. The NULL is untyped in SQL, meaning that it is
not an integer, a character, or any other specific data type.
Note that NULL is not the same as an empty data string or the
numerical value '0'. While NULL indicates the absence of a value, the
empty string and numerical zero both represent actual values.
While a NULL value can be assigned, it can not be equated with
anything, including itself.
Because NULL does not represent or equate to a data type, you cannot
test for NULL values with any comparison operators, such as =, <, or
<>.
The IS NULL and IS NOT NULL operators are used to test for NULL
values.
Do it the other way around. Put the id of the main table in the others like that
question table
--------------
id
text
...
answers table
-------------
id
question_id
text
...
reactions table
---------------
id
question_id
text
...
And question_id is never null. Then you can use a left join to get the results from both tables - one of them will have no results.
select *
from questions q
left join answers a on a.question_id = q.id
left join reactions r on r.question_id = q.id
While #lad2025s answer is good for two columns, if you wanted to extend the method to more than two it can get a bit cumbersome.
A flexible alternative is:
check ((case when answer_id is null then 0 else 1 end +
case when question_id is null then 0 else 1 end ) = 1)
It extends well to checking for a particular count of null (or non-null) values for an arbitrary number of columns.
For example, if you had column_1, column_2, column3, and column_4, and wanted at least 1 of them to be non-null, then:
check ((case when column_1 is null then 0 else 1 end +
case when column_2 is null then 0 else 1 end +
case when column_3 is null then 0 else 1 end +
case when column_4 is null then 0 else 1 end ) >= 1)

ORACLE SQL // IF ELSE?

I got a small problem in my SQL code :
LEFT JOIN
(SELECT *
FROM pilotage_usines_valeurs
WHERE c_indicateur IS NOT NULL) v
ON v.id_usine = d.id_usine
AND v.annee = 2015
AND V.MOIS = D.MOIS
AND V.C_INDICATEUR = pi1.c_indicateur
Sometime pi1.c_indicateur is null. How can i test it and write the line if pi1.c_indicateur is not null don't write it if pi1.c_indicateur is null ?
If I understand your intentions correctly I suggest you add the NOT NULL condition to the ON clause:
LEFT JOIN (SELECT *
FROM PILOTAGE_USINES_VALEURS
WHERE C_INDICATEUR IS NOT NULL) v
ON v.ID_USINE = d.ID_USINE
AND v.ANNEE = 2015
AND v.MOIS = D.MOIS
AND v.C_INDICATEUR = pi1.C_INDICATEUR
AND pi1.C_INDICATEUR IS NOT NULL
NVL is used to substitute null value by something else. Syntax is similar to
NVL (FIELD_TO_BE_TESTED_FOR_NULL, VALUE_IF_NULL);
Option 1:
Replace pi1.c_indicateur when null with some value so that V.C_INDICATEUR will never be equal to that value.
AND V.C_INDICATEUR = NVL(pi1.c_indicateur,'IMPOSSIBLE VALUE FOR V.C_INDICATEUR ');
Option 2:
Replace V.C_INDICATEUR with some value if null and replace pi1.c_indicateur with some other value, so that both will never match if NULL.
AND (V.C_INDICATEUR,'ABC') = NVL(pi1.c_indicateur,'DEF');

Resources