Solved

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

Posted on 2006-11-07
8
1,887 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
Comment Utility
Hi,
You can write a function in Database and return desired size of varchar2.
0
 
LVL 2

Author Comment

by:AlbertYou
Comment Utility

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
Comment Utility
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 2

Author Comment

by:AlbertYou
Comment Utility

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
Comment Utility
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
Comment Utility
AlbertYou,

You can declare function in this way..

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

Author Comment

by:AlbertYou
Comment Utility

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now