Updating column by removing trailing spaces where column has a not null constraint

Hello,
I have a table named documents where it has a column document_nbr  that has a not null constraint.
I want to remove any trailing spaces that are within the document_nbr's.

I have tried different combinations of an update statement but none seem to work, as it violates the not null constraint.
update documents
set document_nbr = trim(document_nbr)

Is there a way to update these columns while still having the not null constraint still being enabled?

I don't want to alter the table or create a temp table and add the records and reinsert them in or any type
of DDL command.

Is there a way to do this just by using an update statement?

Oracle 11g is used.

Thanks
MachinegunnerAsked:
Who is Participating?

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

x
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.

slightwv (䄆 Netminder) Commented:
What value do you want the 'null' values set to?  If the column has all spaces and the not null constraint, it has to contain some value.

Maybe a junk character like '~':
update documents
set document_nbr = nvl(trim(document_nbr),'~')
MachinegunnerAuthor Commented:
Thanks for the reply.
I did try something like this earlier but it didn't work.
update documents
set document_nbr = nvl(rtrim(document_nbr), ' ')

When I try your update, I'm getting a violation key constraint now, so this column
is part of a key...

What do you think if I create a pl/sql procedure that has a cursor to go thru each record
and see if there are any spaces within, then try and update then in a loop?
That way I can add some conditions to check before the updates within the loop.

Thanks
slightwv (䄆 Netminder) Commented:
What conditions would you add in a cursor loop?

What problem are you trying to solve?

If it is just to remove trailing spaces on rows that aren't ALL spaces then:
update documents
set document_nbr = trim(document_nbr)
where trim(document_nbr) is not null

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
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!

MachinegunnerAuthor Commented:
Thanks for the reply,
I tried the update statement and that didn't work.
I believe I was mentioning that I would create exception statement(s) to pass thru records
that instigate the constraints or ignore them.

More thinking about it, if I am removing the spaces and the document_nbr is part of a key,
this would mean that there are document_nbrs already in the system that are <blank> or
have empty spaces already.

So I would need to find these first maybe and see if there are any.
Thinking of trying this statement first and then if there are any records, then fix these first and then
run your update statement above.
Thanks for the help!

Select *
from documents
where nvl(trim(document_nbr), '~') = '~';
MachinegunnerAuthor Commented:
Tried this update statement as the best solution.
I need to clean up some records first then run the dml statement.
Thanks again!
MG
slightwv (䄆 Netminder) Commented:
Glad to help!  We've all had to deal with 'bad' data...  ;)
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.