Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL PLUS FUNCTIONS

Posted on 2003-10-29
6
Medium Priority
?
2,364 Views
Last Modified: 2012-06-21
I HAVE A TABLE NAMED: EMP
TWO COLUMNS: ENAME AND 1RST_OCCUR.
I MUST FIND THE FIRST OCCURRENCE OF 'L' IN ENAME AND CHANGE IT TO 'X'.
SAMPLE:

ENAME             1RST_OCCUR
--------             ----------------

SMITH                 SMITH
ALLEN                  AXLEN
WARD                  WARD
JONES                 JONES
MARTIN               MARTIN
BLAKE                  BXAKE
CLARK                 CXARK
SCOTT                SCOTT
KING                    KING
TURNER               TURNER
ADAMS                ADAMS
JAMES                 JAMES
FORD                   FORD
MILLER                MIXLER
0
Comment
Question by:jheff
[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
6 Comments
 
LVL 12

Expert Comment

by:catchmeifuwant
ID: 9640721
This should do the job...

select ename,
case
when instr(ename,'L') > 0 then
substr(ename,1,instr(ename,'L')-1)||'X'||substr(ename,instr(ename,'L')+1)
else
ename
end
from emp
0
 
LVL 1

Author Comment

by:jheff
ID: 9640741
catchmeifuwant,

is it possible to make another command without using case and else?

thanks.
0
 
LVL 2

Expert Comment

by:racher
ID: 9640764
If you wanted to change all occurences you would use the functions REPLACE or TRANSLATE
As you only want to change the first occurence then you can use INSTR to find the first occurence and then combine that with the use of SUBSTR to get what you want.

ename = SUBSTR(ename, 1, INSTR(ename, 'L' ) -1 ) || 'X' || SUBSTR(ename,INSTR(ename, 'L' ) +1 )

Graham
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 12

Expert Comment

by:catchmeifuwant
ID: 9640925
jheff,


here is an other one using decode...the logic is same as Case When End

select ename,
decode(instr(ename,'L'),0,substr(ename,1,instr(ename,'L')-1)||'X'||substr(ename,instr(ename,'L')+1),ename)
from emp
0
 
LVL 7

Accepted Solution

by:
Bigfam5 earned 300 total points
ID: 9641053
catchmeifuwant you almost had it, except that your returns an extra X in front of the names that do not contain an L

Try this instead

select ename,INSTR(ename, 'L' ),
       decode ( INSTR(ename, 'L'),
                0, ename,
                substr(ename,1, INSTR(ename, 'L' )-1)
                || 'X' ||
                substr(ename, INSTR(ename, 'L' )+1 ) )
 from emp;



0
 
LVL 12

Expert Comment

by:catchmeifuwant
ID: 9641088
:-) Thanks for pointing it out...jumbled the ename and substr !!!
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

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…
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

721 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