Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

trouble in change attribute type from char to varchar2

Posted on 2002-06-06
2
Medium Priority
?
977 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 400 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

824 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