Tanuja Kadam
asked on
Oracle In statement with NVL
Hi ,
I am writing a query in a PL/sql block.
SELECT parid,taxyr,user9,user10
FROM CHARGE
WHERE taxyr = 2014
AND user9 in (select nvl(&¶m2,user9) from charge);
How do I write the instatement with nvl?
I am writing a query in a PL/sql block.
SELECT parid,taxyr,user9,user10
FROM CHARGE
WHERE taxyr = 2014
AND user9 in (select nvl(&¶m2,user9) from charge);
How do I write the instatement with nvl?
ASKER
This select statement is a cursor. &¶m2 consists of multiple values.
and if that filed is null then the field user9 = user9.
I have to use in statement for this.
and if that filed is null then the field user9 = user9.
I have to use in statement for this.
If your in list contains numbers, then this should work.
SELECT parid,
taxyr,
user9,
user10
FROM charge
WHERE taxyr = 2014
AND ( user9 IN ( &¶m2 )
OR Length('&¶m2') = 0 );
I'm assuming you want something that checks if your paramter is null, then get all results for 2014
otherwise, get only those results where user9 matchs your parameter.
If so, try this:
SELECT parid,taxyr,user9,user10
FROM CHARGE
WHERE taxyr = 2014
AND (&¶m2 is null or user9 = &¶m2)
there is a slight functional difference between this and the queries in the previous posts in that it will return values even when user9 is NULL, where as the original and previous query will only return values where user9 is not null (regardless if the param2 field is null or not)
That may or may not be what you want
otherwise, get only those results where user9 matchs your parameter.
If so, try this:
SELECT parid,taxyr,user9,user10
FROM CHARGE
WHERE taxyr = 2014
AND (&¶m2 is null or user9 = &¶m2)
there is a slight functional difference between this and the queries in the previous posts in that it will return values even when user9 is NULL, where as the original and previous query will only return values where user9 is not null (regardless if the param2 field is null or not)
That may or may not be what you want
>>> OR Length('&¶m2') = 0 );
is not a good way to check for a null value
length('') is null, not 0
is not a good way to check for a null value
length('') is null, not 0
ASKER
sdstuber....you go what I want but I get missing expression error.
Also, && Param2 may contains multiple values.
Also, && Param2 may contains multiple values.
You know, I knew that length of null is null. Not sure what I was thinking today.
You cannot use the = it must be in because it is a list of values as stated. A straight = is easy. I'm still trying to figure out a unknown list of values.
You cannot use the = it must be in because it is a list of values as stated. A straight = is easy. I'm still trying to figure out a unknown list of values.
>>> Also, && Param2 may contains multiple values.
give me an example of your usage with real sample data
give me an example of your usage with real sample data
ASKER
Param2 values could be as below,
'9A','10','11b','C'
So the user9 field from CHARGE can be the above values and if the Param2 is null then the And clause should be ignored.
'9A','10','11b','C'
So the user9 field from CHARGE can be the above values and if the Param2 is null then the And clause should be ignored.
I'm making the assumption that if param2 is null it will be set to '', not simply left blank.
SELECT parid,taxyr,user9,user10
FROM CHARGE
WHERE taxyr = 2014
AND (&¶m2 is null or user9 in (&¶m2))
SELECT parid,taxyr,user9,user10
FROM CHARGE
WHERE taxyr = 2014
AND (&¶m2 is null or user9 in (&¶m2))
That won't work. The result of the substitution is:
SELECT parid,taxyr,user9,user10
FROM CHARGE
WHERE taxyr = 2014
AND ( '9A','10','11b','C' is null or user9 in ( '9A','10','11b','C'))
And that results in a ORA-00920: Invalid releational opertator
SELECT parid,taxyr,user9,user10
FROM CHARGE
WHERE taxyr = 2014
AND ( '9A','10','11b','C' is null or user9 in ( '9A','10','11b','C'))
And that results in a ORA-00920: Invalid releational opertator
hah, yeah, you're right.
I had one side figured out for if it was null and the other side for it was not. but didn't take into account the reverse!
I had one side figured out for if it was null and the other side for it was not. but didn't take into account the reverse!
before guessing again.
what will your parameter look like if it is null?
what will your parameter look like if it is null?
ASKER
just null...won't return anything
No worries. I've got a ton of different things I have tried. I cannot figure out how to get both sides working.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Can you please explain what q'[&¶m2]' does?
Also, Ihow the INSTR function would work for IN statement?
Also, Ihow the INSTR function would work for IN statement?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you. I will incorporate the logic in my code.
ASKER
Sorry for the delay. you can close this item.
it's your question, you can close it yourself.
in fact, that's the preferred method.
in fact, that's the preferred method.
ASKER
I don't see any close or "Accepted solution" button.
you don't see "Assisted Solution" and "Best Solution" buttons?
ASKER
Nope
ASKER
see the screenshot attached.
no screen shot attached.
but you have 2 options now.
you can just let it go with the auto close already in progress if you agree
if you do not agree with the close in progress, then click the "request attention" link at bottom of your original question and a moderator will assist you
but you have 2 options now.
you can just let it go with the auto close already in progress if you agree
if you do not agree with the close in progress, then click the "request attention" link at bottom of your original question and a moderator will assist you
Otherwise, I believe this will do what you want, but it isn't really clear to me what you are trying to do.
Open in new window