Home » SQL & PL/SQL » SQL & PL/SQL » Searching a string and replacing with value (Oracle 10g)
Searching a string and replacing with value [message #612069] |
Fri, 11 April 2014 11:27 |
|
sss111ind
Messages: 634 Registered: April 2012 Location: India
|
Senior Member |
|
|
Hi All,
alter table dept add(mask_field varchar2(100),mask_value varchar2(100));
update dept set mask_field='ename,job,',mask_value=q'[ename||x,job||y,]' where deptno=10;
update dept set mask_field='sal',mask_value=q'[xxxx]' where deptno=20;
select empno,decode('sal',mask_field,mask_value,sal) sal,
ename,job,dept.deptno from emp,dept
where emp.deptno=dept.deptno;
I want to put the decode on ename column in the above query, like decode('ename',mask_field,mask_value,ename)
and it should try to find the ename string in the mask_field if found then it should take the first value .The same needs to happen in case of job field also.job is appearing in the second field hence it should take the second value based on the comma separator. It is working fine for single value but for multiple value the problem is occuring. Please help.
Regards,
Nathan
[MERGED by LF]
[Updated on: Thu, 17 April 2014 12:45] by Moderator Report message to a moderator
|
|
|
|
|
|
Re: Searching a string and replacing with value [message #612074 is a reply to message #612072] |
Fri, 11 April 2014 11:48 |
manubatham20
Messages: 566 Registered: September 2010 Location: Seattle, WA, USA
|
Senior Member |
|
|
Try to play with below code. And post a example before you ask any question again.
with temp as
(select 'xxxMANUxxx,BATHAM' mask_field, 'MANU' ename from dual)
select decode(instr(substr(mask_field,1,instr(mask_field,',')),ename),0,'NOT FOUND','FOUND') from temp;
Manu
|
|
|
|
Re: Function to get column value [message #612274 is a reply to message #612272] |
Tue, 15 April 2014 08:46 |
pointers
Messages: 451 Registered: May 2008
|
Senior Member |
|
|
I am not sure about the background of this why you are creating funcion to do this -
however, assign the function IN value (p_str) to l_str and then return, no need to call test_emp from the function as you shown.
Regards,
Pointers
|
|
|
|
Re: Function to get column value [message #612278 is a reply to message #612272] |
Tue, 15 April 2014 09:13 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
Never use PLSQL when it could be done in plain SQL.
You just need to write a query to select the column value and concatenate with the string you want. If your requirement is something else, please elaborate.
|
|
|
Re: Searching a string and replacing with value [message #612345 is a reply to message #612074] |
Wed, 16 April 2014 06:17 |
|
sss111ind
Messages: 634 Registered: April 2012 Location: India
|
Senior Member |
|
|
alter table dept add(mask_field varchar2(100),mask_value varchar2(100));
update DEPT set MASK_FIELD='ename,job,',MASK_VALUE=q'[ename||x,job||y,]' where DEPTNO=10;
update DEPT set MASK_FIELD='sal,',MASK_VALUE=q'[sal||xxxx,]' where DEPTNO=20;
COMMIT;
CREATE OR replace FUNCTION Test_pp(p_str VARCHAR2, p_col VARCHAR2, p_val VARCHAR2)
RETURN VARCHAR2
IS
l_str VARCHAR2(100) := p_str;
l_val VARCHAR2(100) := p_val;
l_col VARCHAR2(100) := p_col;
l_found VARCHAR2(100);
l_fndval VARCHAR2(100);
l_cnt NUMBER := 0;
l_st NUMBER := 0;
BEGIN
WHILE( Instr(l_str, ',') > 0 ) LOOP
l_cnt := l_cnt + 1;
l_found := Substr(l_str, 1, Instr(l_str, ',') - 1);
l_fndval := Substr(l_val, 1, Instr(l_val, ',') - 1);
IF l_found = l_col THEN
l_st := 1;
exit;
END IF;
l_str := Substr(l_str, Instr(l_str, ',') + 1);
l_val := Substr(l_val, Instr(l_val, ',') + 1);
END LOOP;
IF l_st = 0 THEN
RETURN NULL;
ELSE
RETURN l_fndval;
end if;
end;
select NVL(TEST_PP(MASK_FIELD,'sal',MASK_VALUE),SAL) SAL ,
NVL(TEST_PP(MASK_FIELD,'ename',MASK_VALUE),ENAME)ENAME,
NVL(test_pp(mask_field,'job',mask_value),job)job
from emp,dept
where emp.deptno=dept.deptno;
I AM GETTING the EXCEPTED value BUT HOW the ENAME ,SAL,job WILL return the EXACT value of the EMP
table but not the column name itself. Please help.
REGARDS,
Nathan
|
|
|
Re: Searching a string and replacing with value [message #612386 is a reply to message #612345] |
Thu, 17 April 2014 07:34 |
|
sss111ind
Messages: 634 Registered: April 2012 Location: India
|
Senior Member |
|
|
alter table emp add ssn varchar2(15);
update emp set ssn= '101-64-9265' where empno= 7369 ;
update emp set ssn= '106-41-6883' where empno= 7499 ;
update emp set ssn= '115-93-9248' where empno= 7521 ;
update emp set ssn= '130-56-7205' where empno= 7566 ;
update emp set ssn= '162-28-7445' where empno= 7654 ;
update emp set ssn= '163-79-5574' where empno= 7698 ;
update emp set ssn= '165-01-9076' where empno= 7782 ;
update emp set ssn= '175-58-2875' where empno= 7788 ;
update emp set ssn= '187-16-8016' where empno= 7839 ;
update emp set ssn= '192-10-5480' where empno= 7844 ;
update emp set ssn= '302-07-7274' where empno= 7876 ;
update emp set ssn= '327-85-5856' where empno= 7900 ;
update EMP set SSN= '826-73-4221' where EMPNO= 7902 ;
update emp set ssn= '626-73-4298' where empno= 7934 ;
update dept set mask_field= 'ssn~sal~' ,mask_value ='REGEXP_REPLACE(ssn,''(\-[[:digit:]]{4})'',''-XXXX'')~REGEXP_REPLACE(sal,''([[:digit:]](*))'',''X'')~' where deptno=10;
update dept set mask_field= 'ssn~ename~' ,mask_value='REGEXP_REPLACE(ssn,''(\-[[:digit:]]{2}\-)'',''-XX-'')~REGEXP_REPLACE(ename,''([[:alnum:]](*))'',''X'') ~' where deptno=20;
update dept set mask_field= 'ssn~comm~' ,mask_value='REGEXP_REPLACE(ssn,''([[:digit:]]{3}\-)'',''XXX-'')~REGEXP_REPLACE(comm,''([[:digit:]](*))'',''X'')~' where deptno=30;
commit;
CREATE OR replace FUNCTION get_value(p_str IN VARCHAR2)
RETURN VARCHAR2
IS
L_STR varchar2(100);
L_QUERY varchar2(4000);
c1 SYS_REFCURSOR;
BEGIN
IF p_str IS NULL THEN
RETURN NULL;
END IF;
L_QUERY := 'select ' || p_str || ' from dual ';
OPEN c1 FOR L_QUERY;
LOOP
FETCH c1 INTO l_str;
exit WHEN c1%NOTFOUND;
END LOOP;
return L_STR;
END;
--test_pp is same as the above procedure but replace , with ~
SELECT dept.deptno,empno,hiredate,job,
Nvl(Get_value(Replace(Test_pp(mask_field, 'ename', mask_value), 'ename', '''' ||ename ||'''') ), ename) ENAME,
Nvl(Get_value(Replace(Test_pp(mask_field, 'ssn', mask_value), 'ssn', '''' || ssn || '''')), ssn)SSN,
NVL(GET_VALUE(replace(TEST_PP(MASK_FIELD, 'sal', MASK_VALUE), 'sal', '''' || SAL || '''')), SAL) SAL,
Nvl(Get_value(Replace(Test_pp(mask_field, 'comm', mask_value), 'comm', '''' ||comm ||'''')), comm) comm
from EMP, DEPT
WHERE emp.deptno = dept.deptno;
the all DEPTNOS ARE having DIFFERENT columns with CUSTOMIZED data in DEPT.the INTENTION is to FETCH the data ACCORDING to the DEPT.
Please suggest anything wrong in the above process.
[Updated on: Thu, 17 April 2014 07:36] Report message to a moderator
|
|
|
|
|
|
|
|
Goto Forum:
Current Time: Fri Apr 19 13:50:36 CDT 2024
|