How to pass a null value as bind parameter - oracle

I have this table Line with a DEL_IND column. The possible values are Y or null
Im using OBIP, and there is a parameter requirement that allows selection of null or Y.
OBIP do not allow blank in their 'fixed value' menu.
I've tried to enter 'List of Values' in OBIP for No to be '' (empty string), but it doesnt seem to work.
LINE.DEL_IND = :P_DELETION_FLAG << i need to pass the value null for this clause
How do I pass null value selection into the query?

Even if you find the way to pass NULL, this:
WHERE LINE.DEL_IND = :P_DELETION_FLAG
won't work properly. If :P_DELETION_FLAG is NULL, query should look like this:
WHERE (LINE.DEL_IND = :P_DELETION_FLAG or :P_DELETION_FLAG IS NULL)
because
WHERE LINE.DEL_IND = NULL
is invalid; should be
WHERE LINE.DEL_IND IS NULL (or IS NOT NULL)

Related

Java Stream filter - how can i filter data without wrapping the filter code in 'if' condition for checking null on the filtering keys?

I am doing the following:
List<Objects> filtered = objects.stream()
.filter(o -> source.equals(o.getSource()) && date.equals(o.getDate()) && id.equals(o.getId()))
.collect(Collectors.toList());
where both date and id could possibiliy be null, as the are coming from method parameters.
How can I ignore them if null, without wrapping the above code in an if statement tp check 'id' and 'date' for null values ? I want to do it inside the filter.
Edit : To make it more clear, i want the filter to act only on the non-null values, i.e if date is non-null and id is null, then filter only on date and ignore id, and so on..
Thanks
An additional option is to do the null checks in the predicate:
.filter(o -> source.equals(o.getSource())
&& (null == date || date.equals(o.getDate()))
&& (null == id || id.equals(o.getId())))
You could use the static method Objects::equals. Chances are that this method is designed for just this:
List<Objects> filtered = objects.stream()
.filter(o -> Objects.equals(source, o.getSource()) && Objects.equals(date, o.getDate()) && Objects.equals(id, o.getId()))
.collect(Collectors.toList());
Note: as Scratte mentioned in the comments, this may not filter out objects for which getDate() returns null, if date is also null. Same goes for id. If that's the case, then the abovementioned code snippet does not comply. In that case, then we have to explicitly filter for non-null dates and ids:
.filter(o -> Objects.nonNull(o.getId()) && Objects.nonNull(o.getDate()))
Update
If you want to skip the comparison for getDate() if date is null (and same for id), then you could check first for null, just like in ernest_k's answer.
You could easily build a small method for it:
public static boolean nullOrEquals(Object baseObject, Object compareObject) {
return baseObject == null || Objects.equals(baseObject, compareObject);
}
.filter(o -> Objects.equals(source, o.getSource())
&& nullOrEquals(date, o.getDate())
&& nullOrEquals(id, o.getId()))
Here's an Ideone example.

Oracle APEX: Multiple Conditions?

I wish to have a region displayed only if
SELECT * FROM REI_LABOUR_RATE_REQUEST
WHERE BILLING_PARTNER = :P6_FILTER_DEALER
returns at least one row and the element :P6_FILTER_YEAR is not NULL.
I tried this by writing:
SELECT * FROM REI_LABOUR_RATE_REQUEST
WHERE BILLING_PARTNER = :P6_FILTER_DEALER
AND :P6_FILTER_YEAR != NULL;
but that somehow never returns any row.
Did you try having a conditional display on the region with the type being "Value of item IS NOT NULL" and P6_FILTER_DEALER as the expression.
This should work as long as the value of the item is submitted and the item has the value stored in the session.
Region Source:
SELECT * FROM REI_LABOUR_RATE_REQUEST
WHERE BILLING_PARTNER = :P6_FILTER_DEALER
As #Tony Andrews says:
:P6_FILTER_YEAR != NULL will never be true. use :P6_FILTER_YEAR IS NOT
NULL instead. In fact the condition is redundant here, because
BILLING_PARTNER = :P6_FILTER_DEALER will only be true when
:P6_FILTER_YEAR is not null anyway.

Getting MinDate instead of NULL

I am calling a stored procedure, and may expect a NULL back.
However, when I get to EntityFramework with the result, it seems that the result gets converted to a Min Date (01-01-0001).
result.NextPaymentDate =
(from c in Context.GetPaymentDatesForSchedule(source.id) where c.NextPaymentFlag select c.PaymentDate)
.FirstOrDefault();
Is there a way to make it NULL, if I get a NULL value from the sproc?
It seems 'dirty' doing something like:
if (result.NextPaymentDate == DateTime.MinValue)
result.NextPaymentDate = null;
It is because the NextPaymentDate property is not nullable in your model, if you make it nullable you will then get the null value instead of min date.

Unique from list or null in Linq?

I have a list of strings, and want to use a single line of Linq to return the list's unique value (if there is one) or null, otherwise. In other words:
["a","a","a","a","a"] returns "a"
["a"] returns "a"
["a","a","a","a","b"] returns null
["a","b","c"] returns null
[] returns null
I assume I would use IEnumerable.Distinct() to collapse the list to its unique values, but I don't know how to do the "single or null" (SingleOrDefault() throws an exception if there's more than one item; FirstOrDefault() will always return the first item, and won't return null if there's two or more like I want.)
Any ideas? I'm just really curious if there's a simple way to do this in a single line that I'm overlooking. Bonus points if you don't have to write an extension method to make it work. Thanks!
col.Distinct().Count() == 1? col.First() : null;
Would this do?
col.Distinct().Count() > 1 ? null : col.Distinct().First();
Updated to handle empty collection
col.Count() == 0 ? null : col.Distinct().Count() > 1 ? null : col.Distinct().First();

HQL Query with multiple Criteria

I am trying to write a HQL Query which selectes rows from a table based on multiple criteria.
firstName,lastName
the catch is that the query should be flexible to ignore any empty or null values
so
select t from table t where (:firstname = '' or t.firstName = :firstName) AND
(:lastName = '' OR t.lastName = :lastName)
I would have thought this would work? But it doesnt - it never returns any rows? Any ideas what could be wrong here? I am very new to HQL thats why this question.
If I am understanding correctly you want a way to allow the user to search by firstName, lastName or both. So you should be checking if the parameter passed in is empty then don't make it a condition. If they supply all blank parameters it would return the whole table. Try:
select t from table t
where (:firstname IS NULL or t.firstName = :firstName) AND
(:lastName IS NULL OR t.lastName = :lastName)
(:firstname = '' or t.firstName = :firstName)
Your criteria is strange. If :firstname = '' and if a firstname (t.firstName) is equal '' in the database, the criteria t.firstName = :firstName is good ('' = '')
You don't need :firstname = ''
But If you want to check null value, you need to do:
t.firstName IS NULL or t.firstName = :firstname
What happens if you run following hql with firstname parameter set to empty string?
select t from table t where (:firstname = '')
and following with firstname parameter set to null:
select t from table t where (:firstname is null)
If any of the above returns the whole table then the HQLs named parameter might support what you are trying to do.
Otherwise you must use different queries for the null parameter cases. You can do this by generating the query dynamically.
I had a similar requirement. I want dynamic but I'm using a tool that just gives an HQL editor, so no Java.
The query below allows for optional parameters. Essentially a pseudo-quazi XOR of sorts . . . wish there was real XOR :/
With this query you just put NA into a param instead of leaving it empty if it is not needed.
Yeah, yeah, yeah . . . it's ugly, but it works and it's easy to alter to any other scenario needing optional params in pure HQL.
SELECT t AS table
FROM Table t
WHERE (t.valSet = :valSet
AND (:category= 'NA' AND :subCategory= 'NA'))
OR (:category != 'NA'
AND (t.valSet = :valSet
AND t.category= :category))
OR (:subCategory != 'NA'
AND (t.valSet = :valSet
AND t.subCategory = :subCategory ))

Resources