Solved

trouble in change attribute type from char to varchar2

Posted on 2002-06-06
2
961 Views
Last Modified: 2012-06-27
I create a table in oracle 8i and insert many data.

create table item(item_id number,
             location_id char(10));

Later on i changed the location_id from char(10) to varchar2(10)

using alter table modify(location_id varchar2(10))

But when i run the query

select item_id, location_id from item where location_id
= '114A';

I cannot select anything.

Actually there are many rows containing '114A' and it works fine before i make the change.

Please tell me what is wrong and how to fix it.
Thank you.

0
Comment
Question by:huhu
2 Comments
 
LVL 11

Expert Comment

by:pennnn
ID: 7059773
Maybe the reason is the difference between the CHAR and VARCHAR2 datatypes - the length of a CHAR(10) field is always 10, while the length of the VARCHAR2(10) field is the real length of the stored string - in your case ('114A') it would be 4.
So probably when your converted the datatype to VARCHAR2 all the strings got rightpadded with spaces.
If you are sure that your data doesn't contain trailing spaces you might try the following statement:
UPDATE item
SET location_id = RTRIM(location_id);

Then try to run your query again.
Hope that helps!
0
 
LVL 5

Accepted Solution

by:
kelfink earned 100 total points
ID: 7059798
When you stored the column as CHAR, oracle always padded the column out to the defined length, regardless of any extra spaces at the end.

insert into item values ( 1, '114A');
would have the identical effect as:
insert into item values ( 1, '114A ');


So when you converted to VARCHAR2(10),
oracle had two options... either automatically trim all spaces off the end of the columns, or leave the padding on all the rows.  They chose the simpler route, which is probably no more prone to error than if they had trimmed it for you.  After all, you can still trim it yourself, if desired, or let the padding stay and use LIKE.

The following two queries will both find the rows you want
select item_id, location_id from item where location_id
= '114A      ';
and
select item_id, location_id from item where rtrim(location_id) = '114A';

But you probably want to manually trim the columns yourself, to remove the extra padding:

UPDATE ITEM SET LOCATION_ID = rtrim(LOCATION_ID);





0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now