Trim functionality

if I have a variable VARCHAR2(18 BYTE) and occasionally the string has empty space(ie, 'W12345678901      ' ) and after setting it to a variable, I use the variable equal to a field from a table in a query. Will it equal the string with the empty spaces? I am noticing  in certain situations that it isn't? Don't have specifics but can come up with some if needed. Just wanted a little insight to how this works exactly?
I have  since used TRIM on my setting of the variable. I was using TRIM in the query but I  think its taking longer especially when looping through a big record set.
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.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
it's always be advised to NOT saving extra space into database.

if the field already hav values with extra space, u may want to do a data cleaning to maintain the data accuracy and integrity.
slightwv (䄆 Netminder) Commented:
I doubt TRIM is taking noticeable time.

If it is, I'm betting you have larger issues in the database and/or server it is running on.

A quick test from sqlplus:
set timing on
select count(*) from some_table where some_column='abc';
select count(*) from some_table where trim(some_column)='abc';
select count(*) from some_table where some_column='abc';
select count(*) from some_table where trim(some_column)='abc';
set timing off

Do it more than once to nullify caching.

If this is a test system, you might flush the buffer cache between runs.

If you have 11g and result caching enabled, you will need to do flush the result cache as well.

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
jknj72Author Commented:
>>if I have a variable VARCHAR2(18 BYTE)

I meant I have a field that I set to a variable
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!

slightwv (䄆 Netminder) Commented:
I just thought of something:

TRIM, or any function on an indexed column will make the index unusable.

An explain plan will confirm this.

If you need the trim and the index, you need to create a Function Based Index:
slightwv (䄆 Netminder) Commented:
>>I meant I have a field that I set to a variable

Oracle doesn't automatically trim spaces.  If you insert them, then stay...

drop table tab1 purge;
create table tab1(col1 varchar2(10));

insert into tab1 values('a   ');

select ':' || col1 || ':' from tab1;

Open in new window

johnsoneSenior Oracle DBACommented:
For cases like this, we usually put triggers in place to trim the value when it is inserted/updated.  That way there are no issues.  A few lines in a trigger and a one time update and your problem goes away.
jknj72Author Commented:
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.