Solved

trouble in change attribute type from char to varchar2

Posted on 2002-06-06
2
970 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle SQL 6 73
Web Service from a stored procdure oracle 10 58
Oracle 12c Default Isolation Level 17 41
DECLARATION OF CURSOR IS INCOMPLETE OR MALFORMED 5 18
Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

839 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