Link to home
Start Free TrialLog in
Avatar of AlbertYou
AlbertYouFlag for Taiwan, Province of China

asked on

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


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.


Avatar of syed555
syed555

Hi,
You can write a function in Database and return desired size of varchar2.
Avatar of AlbertYou

ASKER


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) ?
ASKER CERTIFIED SOLUTION
Avatar of GGuzdziol
GGuzdziol
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

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 ?

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
AlbertYou,

You can declare function in this way..

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

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.