Alternative for COALESCE when all its parameters is null
I am developing a web application (java-maven project) and there is a problem with the query I use. The UI of my web app has 2 search fields: PTT
and ID
.
The user needs to fill at least one of the fields to make a search. So both fields are nullable but not at the same time.
Before, I had only one field: PTR
and it was showing a result array of size 52. (also getting the same number if
I execute select * from users where ptr='smthing'
). After that I added ID
field and updated my query as below:
I execute this query in my webservice:
String query= "SELECT t.ptr, t.id ";
query+= "FROM users t ";
query+= "WHERE t.ptr = COALESCE(?, t.ptr) AND " ;
query+= "t.id = COALESCE(?, t.id) ";
and set the fields with the help of Prepared Statement.
Now if the ptr
field is filled, but id
field is left blank (this can be null or empty string) on the UI and user makes a search, result array size becomes 30. I compared with database
and it does not fetch the rows where ID
is null. So coalesce
is not what I need when both of its parameters (?, t.ptr) is null.
How can I fix this problem, any suggestions?
java oracle plsql
add a comment |
I am developing a web application (java-maven project) and there is a problem with the query I use. The UI of my web app has 2 search fields: PTT
and ID
.
The user needs to fill at least one of the fields to make a search. So both fields are nullable but not at the same time.
Before, I had only one field: PTR
and it was showing a result array of size 52. (also getting the same number if
I execute select * from users where ptr='smthing'
). After that I added ID
field and updated my query as below:
I execute this query in my webservice:
String query= "SELECT t.ptr, t.id ";
query+= "FROM users t ";
query+= "WHERE t.ptr = COALESCE(?, t.ptr) AND " ;
query+= "t.id = COALESCE(?, t.id) ";
and set the fields with the help of Prepared Statement.
Now if the ptr
field is filled, but id
field is left blank (this can be null or empty string) on the UI and user makes a search, result array size becomes 30. I compared with database
and it does not fetch the rows where ID
is null. So coalesce
is not what I need when both of its parameters (?, t.ptr) is null.
How can I fix this problem, any suggestions?
java oracle plsql
what database ? oracle?
– user7294900
Nov 20 '18 at 11:28
1
By the way, PL/SQL Developer is not an Oracle product and PL/SQL is not a query language.
– William Robertson
Nov 20 '18 at 13:49
add a comment |
I am developing a web application (java-maven project) and there is a problem with the query I use. The UI of my web app has 2 search fields: PTT
and ID
.
The user needs to fill at least one of the fields to make a search. So both fields are nullable but not at the same time.
Before, I had only one field: PTR
and it was showing a result array of size 52. (also getting the same number if
I execute select * from users where ptr='smthing'
). After that I added ID
field and updated my query as below:
I execute this query in my webservice:
String query= "SELECT t.ptr, t.id ";
query+= "FROM users t ";
query+= "WHERE t.ptr = COALESCE(?, t.ptr) AND " ;
query+= "t.id = COALESCE(?, t.id) ";
and set the fields with the help of Prepared Statement.
Now if the ptr
field is filled, but id
field is left blank (this can be null or empty string) on the UI and user makes a search, result array size becomes 30. I compared with database
and it does not fetch the rows where ID
is null. So coalesce
is not what I need when both of its parameters (?, t.ptr) is null.
How can I fix this problem, any suggestions?
java oracle plsql
I am developing a web application (java-maven project) and there is a problem with the query I use. The UI of my web app has 2 search fields: PTT
and ID
.
The user needs to fill at least one of the fields to make a search. So both fields are nullable but not at the same time.
Before, I had only one field: PTR
and it was showing a result array of size 52. (also getting the same number if
I execute select * from users where ptr='smthing'
). After that I added ID
field and updated my query as below:
I execute this query in my webservice:
String query= "SELECT t.ptr, t.id ";
query+= "FROM users t ";
query+= "WHERE t.ptr = COALESCE(?, t.ptr) AND " ;
query+= "t.id = COALESCE(?, t.id) ";
and set the fields with the help of Prepared Statement.
Now if the ptr
field is filled, but id
field is left blank (this can be null or empty string) on the UI and user makes a search, result array size becomes 30. I compared with database
and it does not fetch the rows where ID
is null. So coalesce
is not what I need when both of its parameters (?, t.ptr) is null.
How can I fix this problem, any suggestions?
java oracle plsql
java oracle plsql
edited Nov 22 '18 at 10:57
asked Nov 20 '18 at 11:26
csel
567
567
what database ? oracle?
– user7294900
Nov 20 '18 at 11:28
1
By the way, PL/SQL Developer is not an Oracle product and PL/SQL is not a query language.
– William Robertson
Nov 20 '18 at 13:49
add a comment |
what database ? oracle?
– user7294900
Nov 20 '18 at 11:28
1
By the way, PL/SQL Developer is not an Oracle product and PL/SQL is not a query language.
– William Robertson
Nov 20 '18 at 13:49
what database ? oracle?
– user7294900
Nov 20 '18 at 11:28
what database ? oracle?
– user7294900
Nov 20 '18 at 11:28
1
1
By the way, PL/SQL Developer is not an Oracle product and PL/SQL is not a query language.
– William Robertson
Nov 20 '18 at 13:49
By the way, PL/SQL Developer is not an Oracle product and PL/SQL is not a query language.
– William Robertson
Nov 20 '18 at 13:49
add a comment |
3 Answers
3
active
oldest
votes
I think the logic you want is:
WHERE (t.ptr = ? OR ? IS NULL) AND
(t.id = ? OR ? IS NULL)
I would recommend using named parameters, so you don't have to pass them in twice.
Thank you :) A question: how do we set parameters for "?" here. In old version I set them like PreparedStatement ps = conn.prepareStatement(query); ps.setString(1, obj.getPTT()); ps.setString(1, obj.getID()); I am asking this because now I get "java.sql.SQLException: Missing IN or OUT parameter at index"
– csel
Nov 20 '18 at 11:41
@csel . . . This version has four parameters rather than two, so you need to include all four parameters.
– Gordon Linoff
Nov 20 '18 at 12:58
add a comment |
Check this statement:
String query= "SELECT t.ptr, t.id ";
query+= "FROM users t ";
query+= "WHERE (t.ptr = ? OR 1 = ?)"
query+= " AND " ;
query+= "(t.id = ? OR 1 = ?)";
You see that for each of t.id
and t.ptr
there is a counterpart parameter.
In total there will be 4 parameters.
You say that at least 1 of t.id
or t.ptr
has a valid value, so there are 2 cases:
[1] t.id
and t.ptr
both have valid values.
For both the counterpart parameters you pass 0
and the query becomes:"SELECT t.ptr, t.id FROM users t WHERE (t.ptr = valueptr OR 1 = 0) AND (t.id = valueid OR 1 = 0)"
In the WHERE part: t.ptr = valueptr OR 1 = 0
is equivalent to t.ptr = valueptr
, and t.id = valueid OR 1 = 0
is equivalent to t.id = valueid
,
and the query finally becomes: "SELECT t.ptr, t.id FROM users t WHERE t.ptr = valueptr AND t.id = valueid"
[2] from t.id
or t.ptr
only one has a valid value, let's say this is t.ptr
.
For the counterpart of t.ptr
you pass 0
, for t.id
you pass -1
(or any other non existing value) and for the counterpart of t.id
you pass 1
and the query becomes:"SELECT t.ptr, t.id FROM users t WHERE (t.ptr = valueptr OR 1 = 0) AND (t.id = -1 OR 1 = 1)"
In the WHERE part: t.ptr = valueptr OR 1 = 0
is equivalent to t.ptr = valueptr
, and t.id = -1 OR 1 = 1
is equivalent to true
because 1 = 1
is always true
,
and the query finally becomes: "SELECT t.ptr, t.id FROM users t WHERE (t.ptr = valueptr OR 1 = 0)"
equivalent to: "SELECT t.ptr, t.id FROM users t WHERE (t.ptr = valueptr)"
(In the case where only t.id
has a valid value then you pass an invalid value for t.ptr
and 1
for its counterpart and for the counterpart if t.id
you pass 0
.)
Maybe it seems complicated but it's working and it can be extended for more than 2 columns.
add a comment |
Oracle has build function for this. But it's hard to understand how to use its.
lnnvl(a = b) = true if (a != b ) or ( a = null ) or (b = null)
in your case
WHERE lnnvl(t.ptr != ? ) AND lnnvl( t.id != ?)
LNNVL
I have 2 questions. I used as you say now, it brings null fields which is one of the things I wanted but for example lets say ptr is entered as 'abcdef' and id is left blank it brings all the records with empty ptr and ptr='abcdef' from the DB. The second thing is that how can I use this with "BETWEEN" ? I want to add "query += "t.date BETWEEN COALESCE(?, t.date) AND COALESCE(?,t.date) AND "; " something like this with lnnvl.
– csel
Nov 22 '18 at 10:55
++ I think it is normal behavior of lnnvl to bring all the empty ptr values also. I just want to bring all the records with ptr value 'abcdef' (if the user submitted value for ptr field). But it was not bringing the records which has null ID and ptr='abcdef'
– csel
Nov 22 '18 at 11:08
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391996%2falternative-for-coalesce-when-all-its-parameters-is-null%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think the logic you want is:
WHERE (t.ptr = ? OR ? IS NULL) AND
(t.id = ? OR ? IS NULL)
I would recommend using named parameters, so you don't have to pass them in twice.
Thank you :) A question: how do we set parameters for "?" here. In old version I set them like PreparedStatement ps = conn.prepareStatement(query); ps.setString(1, obj.getPTT()); ps.setString(1, obj.getID()); I am asking this because now I get "java.sql.SQLException: Missing IN or OUT parameter at index"
– csel
Nov 20 '18 at 11:41
@csel . . . This version has four parameters rather than two, so you need to include all four parameters.
– Gordon Linoff
Nov 20 '18 at 12:58
add a comment |
I think the logic you want is:
WHERE (t.ptr = ? OR ? IS NULL) AND
(t.id = ? OR ? IS NULL)
I would recommend using named parameters, so you don't have to pass them in twice.
Thank you :) A question: how do we set parameters for "?" here. In old version I set them like PreparedStatement ps = conn.prepareStatement(query); ps.setString(1, obj.getPTT()); ps.setString(1, obj.getID()); I am asking this because now I get "java.sql.SQLException: Missing IN or OUT parameter at index"
– csel
Nov 20 '18 at 11:41
@csel . . . This version has four parameters rather than two, so you need to include all four parameters.
– Gordon Linoff
Nov 20 '18 at 12:58
add a comment |
I think the logic you want is:
WHERE (t.ptr = ? OR ? IS NULL) AND
(t.id = ? OR ? IS NULL)
I would recommend using named parameters, so you don't have to pass them in twice.
I think the logic you want is:
WHERE (t.ptr = ? OR ? IS NULL) AND
(t.id = ? OR ? IS NULL)
I would recommend using named parameters, so you don't have to pass them in twice.
answered Nov 20 '18 at 11:28
Gordon Linoff
760k35294399
760k35294399
Thank you :) A question: how do we set parameters for "?" here. In old version I set them like PreparedStatement ps = conn.prepareStatement(query); ps.setString(1, obj.getPTT()); ps.setString(1, obj.getID()); I am asking this because now I get "java.sql.SQLException: Missing IN or OUT parameter at index"
– csel
Nov 20 '18 at 11:41
@csel . . . This version has four parameters rather than two, so you need to include all four parameters.
– Gordon Linoff
Nov 20 '18 at 12:58
add a comment |
Thank you :) A question: how do we set parameters for "?" here. In old version I set them like PreparedStatement ps = conn.prepareStatement(query); ps.setString(1, obj.getPTT()); ps.setString(1, obj.getID()); I am asking this because now I get "java.sql.SQLException: Missing IN or OUT parameter at index"
– csel
Nov 20 '18 at 11:41
@csel . . . This version has four parameters rather than two, so you need to include all four parameters.
– Gordon Linoff
Nov 20 '18 at 12:58
Thank you :) A question: how do we set parameters for "?" here. In old version I set them like PreparedStatement ps = conn.prepareStatement(query); ps.setString(1, obj.getPTT()); ps.setString(1, obj.getID()); I am asking this because now I get "java.sql.SQLException: Missing IN or OUT parameter at index"
– csel
Nov 20 '18 at 11:41
Thank you :) A question: how do we set parameters for "?" here. In old version I set them like PreparedStatement ps = conn.prepareStatement(query); ps.setString(1, obj.getPTT()); ps.setString(1, obj.getID()); I am asking this because now I get "java.sql.SQLException: Missing IN or OUT parameter at index"
– csel
Nov 20 '18 at 11:41
@csel . . . This version has four parameters rather than two, so you need to include all four parameters.
– Gordon Linoff
Nov 20 '18 at 12:58
@csel . . . This version has four parameters rather than two, so you need to include all four parameters.
– Gordon Linoff
Nov 20 '18 at 12:58
add a comment |
Check this statement:
String query= "SELECT t.ptr, t.id ";
query+= "FROM users t ";
query+= "WHERE (t.ptr = ? OR 1 = ?)"
query+= " AND " ;
query+= "(t.id = ? OR 1 = ?)";
You see that for each of t.id
and t.ptr
there is a counterpart parameter.
In total there will be 4 parameters.
You say that at least 1 of t.id
or t.ptr
has a valid value, so there are 2 cases:
[1] t.id
and t.ptr
both have valid values.
For both the counterpart parameters you pass 0
and the query becomes:"SELECT t.ptr, t.id FROM users t WHERE (t.ptr = valueptr OR 1 = 0) AND (t.id = valueid OR 1 = 0)"
In the WHERE part: t.ptr = valueptr OR 1 = 0
is equivalent to t.ptr = valueptr
, and t.id = valueid OR 1 = 0
is equivalent to t.id = valueid
,
and the query finally becomes: "SELECT t.ptr, t.id FROM users t WHERE t.ptr = valueptr AND t.id = valueid"
[2] from t.id
or t.ptr
only one has a valid value, let's say this is t.ptr
.
For the counterpart of t.ptr
you pass 0
, for t.id
you pass -1
(or any other non existing value) and for the counterpart of t.id
you pass 1
and the query becomes:"SELECT t.ptr, t.id FROM users t WHERE (t.ptr = valueptr OR 1 = 0) AND (t.id = -1 OR 1 = 1)"
In the WHERE part: t.ptr = valueptr OR 1 = 0
is equivalent to t.ptr = valueptr
, and t.id = -1 OR 1 = 1
is equivalent to true
because 1 = 1
is always true
,
and the query finally becomes: "SELECT t.ptr, t.id FROM users t WHERE (t.ptr = valueptr OR 1 = 0)"
equivalent to: "SELECT t.ptr, t.id FROM users t WHERE (t.ptr = valueptr)"
(In the case where only t.id
has a valid value then you pass an invalid value for t.ptr
and 1
for its counterpart and for the counterpart if t.id
you pass 0
.)
Maybe it seems complicated but it's working and it can be extended for more than 2 columns.
add a comment |
Check this statement:
String query= "SELECT t.ptr, t.id ";
query+= "FROM users t ";
query+= "WHERE (t.ptr = ? OR 1 = ?)"
query+= " AND " ;
query+= "(t.id = ? OR 1 = ?)";
You see that for each of t.id
and t.ptr
there is a counterpart parameter.
In total there will be 4 parameters.
You say that at least 1 of t.id
or t.ptr
has a valid value, so there are 2 cases:
[1] t.id
and t.ptr
both have valid values.
For both the counterpart parameters you pass 0
and the query becomes:"SELECT t.ptr, t.id FROM users t WHERE (t.ptr = valueptr OR 1 = 0) AND (t.id = valueid OR 1 = 0)"
In the WHERE part: t.ptr = valueptr OR 1 = 0
is equivalent to t.ptr = valueptr
, and t.id = valueid OR 1 = 0
is equivalent to t.id = valueid
,
and the query finally becomes: "SELECT t.ptr, t.id FROM users t WHERE t.ptr = valueptr AND t.id = valueid"
[2] from t.id
or t.ptr
only one has a valid value, let's say this is t.ptr
.
For the counterpart of t.ptr
you pass 0
, for t.id
you pass -1
(or any other non existing value) and for the counterpart of t.id
you pass 1
and the query becomes:"SELECT t.ptr, t.id FROM users t WHERE (t.ptr = valueptr OR 1 = 0) AND (t.id = -1 OR 1 = 1)"
In the WHERE part: t.ptr = valueptr OR 1 = 0
is equivalent to t.ptr = valueptr
, and t.id = -1 OR 1 = 1
is equivalent to true
because 1 = 1
is always true
,
and the query finally becomes: "SELECT t.ptr, t.id FROM users t WHERE (t.ptr = valueptr OR 1 = 0)"
equivalent to: "SELECT t.ptr, t.id FROM users t WHERE (t.ptr = valueptr)"
(In the case where only t.id
has a valid value then you pass an invalid value for t.ptr
and 1
for its counterpart and for the counterpart if t.id
you pass 0
.)
Maybe it seems complicated but it's working and it can be extended for more than 2 columns.
add a comment |
Check this statement:
String query= "SELECT t.ptr, t.id ";
query+= "FROM users t ";
query+= "WHERE (t.ptr = ? OR 1 = ?)"
query+= " AND " ;
query+= "(t.id = ? OR 1 = ?)";
You see that for each of t.id
and t.ptr
there is a counterpart parameter.
In total there will be 4 parameters.
You say that at least 1 of t.id
or t.ptr
has a valid value, so there are 2 cases:
[1] t.id
and t.ptr
both have valid values.
For both the counterpart parameters you pass 0
and the query becomes:"SELECT t.ptr, t.id FROM users t WHERE (t.ptr = valueptr OR 1 = 0) AND (t.id = valueid OR 1 = 0)"
In the WHERE part: t.ptr = valueptr OR 1 = 0
is equivalent to t.ptr = valueptr
, and t.id = valueid OR 1 = 0
is equivalent to t.id = valueid
,
and the query finally becomes: "SELECT t.ptr, t.id FROM users t WHERE t.ptr = valueptr AND t.id = valueid"
[2] from t.id
or t.ptr
only one has a valid value, let's say this is t.ptr
.
For the counterpart of t.ptr
you pass 0
, for t.id
you pass -1
(or any other non existing value) and for the counterpart of t.id
you pass 1
and the query becomes:"SELECT t.ptr, t.id FROM users t WHERE (t.ptr = valueptr OR 1 = 0) AND (t.id = -1 OR 1 = 1)"
In the WHERE part: t.ptr = valueptr OR 1 = 0
is equivalent to t.ptr = valueptr
, and t.id = -1 OR 1 = 1
is equivalent to true
because 1 = 1
is always true
,
and the query finally becomes: "SELECT t.ptr, t.id FROM users t WHERE (t.ptr = valueptr OR 1 = 0)"
equivalent to: "SELECT t.ptr, t.id FROM users t WHERE (t.ptr = valueptr)"
(In the case where only t.id
has a valid value then you pass an invalid value for t.ptr
and 1
for its counterpart and for the counterpart if t.id
you pass 0
.)
Maybe it seems complicated but it's working and it can be extended for more than 2 columns.
Check this statement:
String query= "SELECT t.ptr, t.id ";
query+= "FROM users t ";
query+= "WHERE (t.ptr = ? OR 1 = ?)"
query+= " AND " ;
query+= "(t.id = ? OR 1 = ?)";
You see that for each of t.id
and t.ptr
there is a counterpart parameter.
In total there will be 4 parameters.
You say that at least 1 of t.id
or t.ptr
has a valid value, so there are 2 cases:
[1] t.id
and t.ptr
both have valid values.
For both the counterpart parameters you pass 0
and the query becomes:"SELECT t.ptr, t.id FROM users t WHERE (t.ptr = valueptr OR 1 = 0) AND (t.id = valueid OR 1 = 0)"
In the WHERE part: t.ptr = valueptr OR 1 = 0
is equivalent to t.ptr = valueptr
, and t.id = valueid OR 1 = 0
is equivalent to t.id = valueid
,
and the query finally becomes: "SELECT t.ptr, t.id FROM users t WHERE t.ptr = valueptr AND t.id = valueid"
[2] from t.id
or t.ptr
only one has a valid value, let's say this is t.ptr
.
For the counterpart of t.ptr
you pass 0
, for t.id
you pass -1
(or any other non existing value) and for the counterpart of t.id
you pass 1
and the query becomes:"SELECT t.ptr, t.id FROM users t WHERE (t.ptr = valueptr OR 1 = 0) AND (t.id = -1 OR 1 = 1)"
In the WHERE part: t.ptr = valueptr OR 1 = 0
is equivalent to t.ptr = valueptr
, and t.id = -1 OR 1 = 1
is equivalent to true
because 1 = 1
is always true
,
and the query finally becomes: "SELECT t.ptr, t.id FROM users t WHERE (t.ptr = valueptr OR 1 = 0)"
equivalent to: "SELECT t.ptr, t.id FROM users t WHERE (t.ptr = valueptr)"
(In the case where only t.id
has a valid value then you pass an invalid value for t.ptr
and 1
for its counterpart and for the counterpart if t.id
you pass 0
.)
Maybe it seems complicated but it's working and it can be extended for more than 2 columns.
answered Nov 20 '18 at 12:17
forpas
9,1691421
9,1691421
add a comment |
add a comment |
Oracle has build function for this. But it's hard to understand how to use its.
lnnvl(a = b) = true if (a != b ) or ( a = null ) or (b = null)
in your case
WHERE lnnvl(t.ptr != ? ) AND lnnvl( t.id != ?)
LNNVL
I have 2 questions. I used as you say now, it brings null fields which is one of the things I wanted but for example lets say ptr is entered as 'abcdef' and id is left blank it brings all the records with empty ptr and ptr='abcdef' from the DB. The second thing is that how can I use this with "BETWEEN" ? I want to add "query += "t.date BETWEEN COALESCE(?, t.date) AND COALESCE(?,t.date) AND "; " something like this with lnnvl.
– csel
Nov 22 '18 at 10:55
++ I think it is normal behavior of lnnvl to bring all the empty ptr values also. I just want to bring all the records with ptr value 'abcdef' (if the user submitted value for ptr field). But it was not bringing the records which has null ID and ptr='abcdef'
– csel
Nov 22 '18 at 11:08
add a comment |
Oracle has build function for this. But it's hard to understand how to use its.
lnnvl(a = b) = true if (a != b ) or ( a = null ) or (b = null)
in your case
WHERE lnnvl(t.ptr != ? ) AND lnnvl( t.id != ?)
LNNVL
I have 2 questions. I used as you say now, it brings null fields which is one of the things I wanted but for example lets say ptr is entered as 'abcdef' and id is left blank it brings all the records with empty ptr and ptr='abcdef' from the DB. The second thing is that how can I use this with "BETWEEN" ? I want to add "query += "t.date BETWEEN COALESCE(?, t.date) AND COALESCE(?,t.date) AND "; " something like this with lnnvl.
– csel
Nov 22 '18 at 10:55
++ I think it is normal behavior of lnnvl to bring all the empty ptr values also. I just want to bring all the records with ptr value 'abcdef' (if the user submitted value for ptr field). But it was not bringing the records which has null ID and ptr='abcdef'
– csel
Nov 22 '18 at 11:08
add a comment |
Oracle has build function for this. But it's hard to understand how to use its.
lnnvl(a = b) = true if (a != b ) or ( a = null ) or (b = null)
in your case
WHERE lnnvl(t.ptr != ? ) AND lnnvl( t.id != ?)
LNNVL
Oracle has build function for this. But it's hard to understand how to use its.
lnnvl(a = b) = true if (a != b ) or ( a = null ) or (b = null)
in your case
WHERE lnnvl(t.ptr != ? ) AND lnnvl( t.id != ?)
LNNVL
answered Nov 20 '18 at 12:58
Arkadiusz Łukasiewicz
4,7791613
4,7791613
I have 2 questions. I used as you say now, it brings null fields which is one of the things I wanted but for example lets say ptr is entered as 'abcdef' and id is left blank it brings all the records with empty ptr and ptr='abcdef' from the DB. The second thing is that how can I use this with "BETWEEN" ? I want to add "query += "t.date BETWEEN COALESCE(?, t.date) AND COALESCE(?,t.date) AND "; " something like this with lnnvl.
– csel
Nov 22 '18 at 10:55
++ I think it is normal behavior of lnnvl to bring all the empty ptr values also. I just want to bring all the records with ptr value 'abcdef' (if the user submitted value for ptr field). But it was not bringing the records which has null ID and ptr='abcdef'
– csel
Nov 22 '18 at 11:08
add a comment |
I have 2 questions. I used as you say now, it brings null fields which is one of the things I wanted but for example lets say ptr is entered as 'abcdef' and id is left blank it brings all the records with empty ptr and ptr='abcdef' from the DB. The second thing is that how can I use this with "BETWEEN" ? I want to add "query += "t.date BETWEEN COALESCE(?, t.date) AND COALESCE(?,t.date) AND "; " something like this with lnnvl.
– csel
Nov 22 '18 at 10:55
++ I think it is normal behavior of lnnvl to bring all the empty ptr values also. I just want to bring all the records with ptr value 'abcdef' (if the user submitted value for ptr field). But it was not bringing the records which has null ID and ptr='abcdef'
– csel
Nov 22 '18 at 11:08
I have 2 questions. I used as you say now, it brings null fields which is one of the things I wanted but for example lets say ptr is entered as 'abcdef' and id is left blank it brings all the records with empty ptr and ptr='abcdef' from the DB. The second thing is that how can I use this with "BETWEEN" ? I want to add "query += "t.date BETWEEN COALESCE(?, t.date) AND COALESCE(?,t.date) AND "; " something like this with lnnvl.
– csel
Nov 22 '18 at 10:55
I have 2 questions. I used as you say now, it brings null fields which is one of the things I wanted but for example lets say ptr is entered as 'abcdef' and id is left blank it brings all the records with empty ptr and ptr='abcdef' from the DB. The second thing is that how can I use this with "BETWEEN" ? I want to add "query += "t.date BETWEEN COALESCE(?, t.date) AND COALESCE(?,t.date) AND "; " something like this with lnnvl.
– csel
Nov 22 '18 at 10:55
++ I think it is normal behavior of lnnvl to bring all the empty ptr values also. I just want to bring all the records with ptr value 'abcdef' (if the user submitted value for ptr field). But it was not bringing the records which has null ID and ptr='abcdef'
– csel
Nov 22 '18 at 11:08
++ I think it is normal behavior of lnnvl to bring all the empty ptr values also. I just want to bring all the records with ptr value 'abcdef' (if the user submitted value for ptr field). But it was not bringing the records which has null ID and ptr='abcdef'
– csel
Nov 22 '18 at 11:08
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391996%2falternative-for-coalesce-when-all-its-parameters-is-null%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
what database ? oracle?
– user7294900
Nov 20 '18 at 11:28
1
By the way, PL/SQL Developer is not an Oracle product and PL/SQL is not a query language.
– William Robertson
Nov 20 '18 at 13:49