Solved

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

Posted on 2006-11-07
8
1,908 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 125 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
Independent Software Vendors: 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!

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

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…
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 copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

751 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