1

i have to find out INPUT string word found within the other string that is pipe delimited,i am trying below way but it is surprisingly return 'Y' instead of 'N'.please let me know what i am doing in wrong in below cast statement.

    CASE
      WHEN REGEXP_INSTR('TCS|XY|XZ','CS',1,1,1,'i') > 0
      THEN 'Y'
      ELSE 'N'
    END 

Regards,

Raj

2 Answers 2

3

There is really no need to use regexp_instr() regular expression function. If you just need to know if a particular character literal is part of another character literal, instr() function will completely cover your needs:

with t1(col) as(
   select 'TCS|XY|XZ' from dual union all
   select 'TAB|XY|XZ' from dual
)
select col
     , case
         when instr(col, 'CS') > 0
         then 'Y'
         else 'N'
       end as Is_Part
 from t1

Result:

COL        IS_PART
---------  -------
TCS|XY|XZ  Y
TAB|XY|XZ  N

Edit


If you need to take vertical bars into consideration - returning yes only if there is a standalone CS sub-string surrounded by vertical bars |CS| then yes, you could use regexp_instr() regular expression function as follows:

 with t1(col) as(
    select 'TCS|XY|XZ|' from dual
  )
  select col
       , case
           when regexp_instr(col, '(\||^)CS(\||$)', 1, 1, 0, 'i') > 0
           then 'YES'
           else 'NO'
         end as res
   from t1

Result:

COL        RES
---------- ---
TCS|XY|XZ| NO

Note: If a character literal is dynamic you could use a concatenation operator || to form a search pattern '(\||^)' || <<'character literal', column or variable>> || '(\||$)'

Sign up to request clarification or add additional context in comments.

4 Comments

THis is not correct,select case when instr('TCS', 'CS') > 0 then 'Y' else 'N' end as Is_Part FROM DUAL .this is returning Still 'Y'
@raj Of course it will return yes, because CS is part of TCS. If there are different criteria of when to return yes and when to return no, you need to state it clearly in the question.
I want to search exact word.If input string is TCS then only it should return yes.if input string is 'CS' or TC.then it should return No.basically it should check exact word.
It works great,how to pass character literal dynamically i.e CS
1

The first field (TCS) contains CS which counts as a match.

If you want to match an entire field you can do like this:

CASE
  WHEN REGEXP_INSTR('|' || 'TCS|XY|XZ' || '|' , '\|' || 'CS' || '\|',1,1,1,'i') > 0
  THEN 'Y'
  ELSE 'N'
END 

Add the delimiter to your query string to "anchor" the search to whole fields. To be able to match the first and last field I also added the delimiter to the searched string.

2 Comments

THis is also faling.CASE WHEN REGEXP_INSTR('|' || 'TCS|XY|XZ' || '|' , '|' || 'CS' || '|',1,1,1,'i') > 0 THEN 'Y' ELSE 'N' END returning 'Y'
I forgot to backslash the | in the regex. I fixed it now and tried it, and it returns N for CS and Y for TCS.

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.