A question related to NVL

Hi Experts,

I have a question regarding Using functions in where clasue, in fact it is a question for where clause

Suppos, I have a query like

SELECT    count ( ROWID )
   FROM    table1
  WHERE   Upper ( Trim ( col1 ) ) = NVL ( :as_para, '' )

Now is the NVL operation performed for every row in table1 while comparing OR it is performed only once and then used for all the rows later

Thanks in advance

LVL 10
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Quite Intresting question...

To my knowledge:

the NVL function will be performed for each and every row.
I am sure that Oracle will not calculate NVL function in every row selection if the argument is a bind variable (parameter).
But if you hesitate you can ensure that it is so. I base my consideration on the fact you use bind variable.
It seems to be parameter in a procedure or item in Forms, reports

Do this

v_param     Varchar2(90) :=  NVL ( :as_para, '' ) ;
SELECT    count ( ROWID )
  FROM    table1
 WHERE   Upper ( Trim ( col1 ) ) = v_param    

You have not to bother about the function NVL. It is very simple gunction.
In contrast to SIN(X) = x/1! - x**3/3! +x**5/5! + ..........
and other "monster" functions.

It will only be computed once.  The thing you will have to worry about with this one is what you are doing to the col1 column, Upper ( Trim ( col1 ) ) will be done for every row.  

A better performing option (sometimes) would be to do:

SELECT    count (1)
   FROM    table1
  WHERE   :as_para IS NULL
        OR   Upper ( Trim ( col1 ) ) = :as_para

So at least you wouldn't be doing your upper(trim()) if the passed in value is null - but it would depend on how often it is run with null, or when not null.  Ah, but in fact you're using bind variables, so, that's sometimes a bit of a grey area (depending on which version of Oracle you are using).  If it is vital that the text be in the Upper'ed format and trimmed, it may be worth putting a function based index on the table, or the possibility of converting the value to always be uppercased, and trimmed when inserted into the database (possibly by a trigger).

There were a couple of posts on here recently for the use of OR and the bind variables stuff, I'll try to find them for you...
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

As mentioned in previous post, this may help you (or be completely useless) :)

Sys_ProgAuthor Commented:
Hi Guys,

Thanks for your quick response

I am using Oracle 9i and yes, it is a local variable. Also, I am trigerring this Select statement as an embedded SQL from PB.

In this case, will the NVL be executed on every row OR only once. In either case, if someone could provide me with some link, it would be of great help

Thanks in adv

Just another testcase to get you thinking:
Table Test with 1 column with numbers with 4 rows
Function testrow:
create or replace function testrow (p_in number) return number
  return p_in;

select * from table where p_in = testrow(:b);   --> :b_in is 10

3 rows returned and client_info-column in V$session says 4
the function is executed for every row.

When you index the column, then you see that the function is not executed for every row.
NVL is just another (in this case kernel-)function.
In your case I would say the NVL goes for every row

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

The NVL function will, without any doubts, perform for each and every row. You using bind variable has nothing to do with the number of times it executes, it only affect the number of the parse times.
Sys_ProgAuthor Commented:
Hi all,

logically speaking, oracle should execute only once because it's a variable and not a column.

vanmeerendonk's  answer was really really good, just have one doubt after that, are user-defined functions treated the same way as the built-in functions.

Thanks in adv

Sys_ProgAuthor Commented:
Hi Guys,

Not yet confirmed of the answer but really liked  vanmeerendonk's approach,

Therefore accepted his answer [It's for his approach and not the answer because I am yet to confirm that]


Thanks again


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.