2

From front end User will enter (multiple) employee number as (123,456,789) to search in the application. The way User enters the employee numbers is pre-defined like above.

This value has to be searched in a Oracle database table in a PL/SQL package.

I have coded like below to format the entered value so that it can be searched in the SQL WHERE condition.

lv_where := REPLACE( REPLACE(  REPLACE( '(123,456,789)', ',', ''',''' ), '(', '(''') , ')', ''')')

Output is : ('123','456','789')

Is there a better way?

2 Answers 2

3

you can use regexp_replace

regexp_replace('(123,456,789)','([[:digit:]]+)','''\1''')
Result:
('123','456','789')
Sign up to request clarification or add additional context in comments.

2 Comments

Right, but - that won't help in what the OP said - use it in WHERE clause :)
but he not said, how he use it in the where clause. maybe he use dynamic sql :)
2

Unless it is dynamic SQL, such a lv_where won't work. I'd suggest you to split entered values into rows and use them in your query.

In the following example, par_emp represents value entered by user. Basically, what you need begins at line #3.

SQL> with test (par_emp) as
  2    (select '(10,20,30)' from dual)
  3  select deptno, ename
  4  from emp
  5  where deptno in (select regexp_substr(replace(replace(par_emp, '(', ''), ')', ''), '[^,]+', 1, level)
  6                   from test
  7                   connect by level <= regexp_count(par_emp, ',') + 1
  8                  )
  9  order by deptno;

    DEPTNO ENAME
---------- ----------
        10 CLARK
        10 KING
        10 MILLER
        20 JONES
        20 FORD
        20 ADAMS
        20 SMITH
        20 SCOTT
        30 WARD
        30 TURNER
        30 ALLEN
        30 JAMES
        30 BLAKE
        30 MARTIN

14 rows selected.

SQL>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.