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

x
?
Solved

Cast a varchar2(4000) column as varchar2(200)

Posted on 2006-11-07
8
Medium Priority
?
1,934 Views
Last Modified: 2007-11-11

Hi you experts,

I have a view looks like the following.

create or repace view EMP_V as
   select empno,
            ename,
            concate_addr( city,address1,address2)  addr_desc
     from emp;

The addr_desc is now defined as varchar2(4000) by the database.
I know that I can cast it into varchar2(200) by using the CAST function in Oracle9i/10g:
    cast(concate_addr( city,address1,address2) as varchar2(200))
But how can I do it in Oracle8i ?

Thanks in advance.


0
Comment
Question by:AlbertYou
  • 3
  • 2
  • 2
8 Comments
 
LVL 2

Expert Comment

by:syed555
ID: 17895332
Hi,
You can write a function in Database and return desired size of varchar2.
0
 
LVL 2

Author Comment

by:AlbertYou
ID: 17896071

Hi syed555,

I know that the concate_addr function can return a specified size of varchar2.
But it is not legal to declare the varchar2 length when creating functions:

function concate_addr(......) return varchar2(200) is
......

Whenever selecting a varchar2 function as a view column,
the column length is automatically set to 4000 by the database.

My question is: how to cast the declared varchar2(4000) view column into varchar2(200) ?
0
 
LVL 14

Accepted Solution

by:
GGuzdziol earned 375 total points
ID: 17896114
I don't have Oracle 8i now...but in 9i adding SUBSTR works:

create or replace view ...
as
select ...,
    substr(..., 1, 200) addr_desc,
    ...
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 2

Author Comment

by:AlbertYou
ID: 17896195

Hi GGuzdziol,

Thanks for your kind help.
The substr function seems working.

However, this might consume CPU resources whenever selecting the column from the view.
Is there any way to "declare" the length a view column which is returned by a function ?

0
 
LVL 14

Assisted Solution

by:GGuzdziol
GGuzdziol earned 375 total points
ID: 17896234
You can oncatenate values in view's code so that Oracle can predict what is maximum size of the expression, i.e. instead of
concate_addr( city,address1,address2)
write
city || <some delim here i.e. chr(13) || chr(10)> || address ...
0
 
LVL 2

Expert Comment

by:syed555
ID: 17904196
AlbertYou,

You can declare function in this way..

Function myFun return Tabl.Col%Type is
begin
...
end;
0
 
LVL 2

Author Comment

by:AlbertYou
ID: 17911931

Hi syed555,

Thanks for your input.

I can create a function returning Tabl.Col%Type as you metioned.
However, when I include the function in the SELECT column list of a view,
the view still declares the type of the column as VARCHAR2(4000).

What I want is to declare the column type as Tabl.Col%Type
without paying any performance overheads.
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

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.  …
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
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.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
Suggested Courses

886 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