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.
jknj72Asked:
Who is Participating?
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 ChongCommented:
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.
0
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.
0

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
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy 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:
https://docs.oracle.com/cd/E11882_01/appdev.112/e41502/adfns_indexes.htm#ADFNS00505
0
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   ');
commit;

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

Open in new window

0
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.
0
jknj72Author Commented:
thanks
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.