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

x
Solved

SQL PLUS FUNCTIONS

Posted on 2003-10-29
Medium Priority
2,366 Views
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
JAMES                 JAMES
FORD                   FORD
MILLER                MIXLER
0
Question by:jheff

LVL 12

Expert Comment

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

ID: 9640741
catchmeifuwant,

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

thanks.
0

LVL 2

Expert Comment

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

LVL 12

Expert Comment

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

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

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

ID: 9641088
:-) Thanks for pointing it out...jumbled the ename and substr !!!
0

Featured Post

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For manyâ€¦
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
Suggested Courses
Course of the Month5 days, 20 hours left to enroll