Home » SQL & PL/SQL » SQL & PL/SQL » Exact String Match in Instr (Oracle 11g)
Exact String Match in Instr [message #641476] Sun, 16 August 2015 10:27 Go to next message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
Hi All,

I want to search exact match in Instr ..for example

Query:

    
    with t as 
    (select '12345:1' a from dual
    union
    select '12345:2' a from dual
    union
    select '12345:112' a from dual
    union
    select '12345:10' a from dual)
    select a, instr(a,'12345:1',1) b  from t


I want Output like:
---a -------b--- 
12345:1     1


if string match then only query will return result otherwise its not. Please help me out ..

Thanks,
Xandot

[EDITED by LF: applied [code] tags to desired output]

[Updated on: Mon, 17 August 2015 00:12] by Moderator

Report message to a moderator

Re: Exact String Match in Instr [message #641477 is a reply to message #641476] Sun, 16 August 2015 10:47 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
If you want an exact match, why are you using INSTR?

I don't understand what problem you are really trying to solve.
Re: Exact String Match in Instr [message #641478 is a reply to message #641477] Sun, 16 August 2015 10:54 Go to previous messageGo to next message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
Is it possible to search exact string using INSTR?

If its not then please suggest me other solution.

Re: Exact String Match in Instr [message #641479 is a reply to message #641478] Sun, 16 August 2015 11:35 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>If its not then please suggest me other solution.

Since I do not understand what problem you are trying to solve, I am unable to offer any solution.

Explain in words what problem needs to be solved.
What EXACTLY needs to be "matched"?
From where do the two objects originate?
Are the two objects always strings?
Re: Exact String Match in Instr [message #641480 is a reply to message #641476] Sun, 16 August 2015 12:05 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

If you want rows with string containing a value:
WHERE INSTR... != 0

If you want rows with exact value:
WHERE REGEXP_LIKE(val,'^...$')

[Updated on: Sun, 16 August 2015 12:06]

Report message to a moderator

Re: Exact String Match in Instr [message #641481 is a reply to message #641480] Sun, 16 August 2015 12:07 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

SQL> with t as
  2      (select '12345:1' a from dual
  3      union
  4      select '12345:2' a from dual
  5      union
  6      select '12345:112' a from dual
  7      union
  8      select '12345:10' a from dual)
  9  select * from t
 10  where regexp_like(a, '^12345:1$')
 11  /
A
---------
12345:1

Re: Exact String Match in Instr [message #641499 is a reply to message #641481] Mon, 17 August 2015 02:53 Go to previous messageGo to next message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
Problem is:

When I search '12345:10' string in query then its return me '12345:1' & '12345:10'. but it should be return only '12345:10'.
Re: Exact String Match in Instr [message #641501 is a reply to message #641499] Mon, 17 August 2015 02:59 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Quote:
When I search '12345:10' string in query then its return me '12345:1' & '12345:10'.


1/ This is not true, just copy and paste your session as I did... if you can
2/ It works if you do what I said.

[Updated on: Mon, 17 August 2015 03:00]

Report message to a moderator

Re: Exact String Match in Instr [message #641502 is a reply to message #641499] Mon, 17 August 2015 02:59 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
Xandot wrote on Mon, 17 August 2015 08:53
Problem is:

When I search '12345:10' string in query then its return me '12345:1' & '12345:10'. but it should be return only '12345:10'.

Show us the code that you're using, along with a test case of sample data and the expected results based on that sample data.
Re: Exact String Match in Instr [message #641510 is a reply to message #641476] Mon, 17 August 2015 06:01 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3305
Registered: January 2010
Location: Connecticut, USA
Senior Member
Last time I checked exact match was called equality:

SQL> with t as 
  2      (select '12345:1' a from dual
  3      union
  4      select '12345:2' a from dual
  5      union
  6      select '12345:112' a from dual
  7      union
  8      select '12345:10' a from dual)
  9      select a, 1 b from t where a = '12345:1'
 10  /

A                  B
--------- ----------
12345:1            1

SQL> 


SY.
Re: Exact String Match in Instr [message #641512 is a reply to message #641510] Mon, 17 August 2015 06:18 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Laughing

Re: Exact String Match in Instr [message #641545 is a reply to message #641476] Mon, 17 August 2015 12:17 Go to previous messageGo to next message
DK555
Messages: 5
Registered: August 2015
Junior Member
This should work

with t as
(select '12345:1' a from dual
union
select '12345:2' a from dual
union
select '12345:112' a from dual
union
select '12345:10' a from dual)
select a, instr(a,'12345:1',1) b from t where a=(select '12345:1' from dual);
Re: Exact String Match in Instr [message #641546 is a reply to message #641545] Mon, 17 August 2015 12:20 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Sorry but this is completely silly.

Re: Exact String Match in Instr [message #641547 is a reply to message #641546] Mon, 17 August 2015 12:42 Go to previous messageGo to next message
DK555
Messages: 5
Registered: August 2015
Junior Member
I can give another silly answer Smile

with t as
(select '12345:1' a from dual
union
select '12345:2' a from dual
union
select '12345:112' a from dual
union
select '12345:10' a from dual)
select a, instr(a,'&&b',1) b from t where a=(select '&b' from dual);

The bottom line is equality is required. The regex expression provided in the above solution also does the exact same equality operation where the value is hard-coded.

All these answers are provided because the concerned person asking the question has not provided the full detail like mode of input for example. There will be multiple answers to 1 problem if the requirement is not specific.
Re: Exact String Match in Instr [message #641548 is a reply to message #641547] Mon, 17 August 2015 12:47 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

You can give more silly answer like:
with t as
(select '12345:1' a from dual
union
select '12345:2' a from dual
union
select '12345:112' a from dual
union
select '12345:10' a from dual)
select a, instr(a,'&&b',1) b from t
where a=(select * from (select * from (select * from (select * from (select * from ((select * from (select * from (select * from (select * from (select * from (select '&b' from dual))))))))))));
Re: Exact String Match in Instr [message #641549 is a reply to message #641548] Mon, 17 August 2015 13:07 Go to previous messageGo to next message
DK555
Messages: 5
Registered: August 2015
Junior Member
No. I wouldn't go that far.

@Michel: Can you tell me why the below answer is silly,

with t as
(select '12345:1' a from dual
union
select '12345:2' a from dual
union
select '12345:112' a from dual
union
select '12345:10' a from dual)
select a, instr(a,'&&b',1) b from t where a=(select '&b' from dual);

It gets the input from user. User gets the required output for any input.It passes all testcases. If his code is in PLSQL, he has to declare a DEFINE variable for this, thats it.

I will point out the problem in your solution as below,
select * from t where regexp_like(a, '^12345:1$')
is the same as below,
select a, 1 b from t where a = '12345:1'

Just that the above uses a regex function with a hardcoded value and returns output only for 1 scenario. The test-case will fail for 12345:2 and 12345:112 and 12345:10 . Impress me if otherwise

Re: Exact String Match in Instr [message #641550 is a reply to message #641549] Mon, 17 August 2015 13:12 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Quote:
Can you tell me why the below answer is silly,


See my previous answer and try to think about it.

Quote:
I will point out the problem in your solution as below,... is the same as below,...


I know this is why I laughed when Solomon posted his remark.

But, you, don't you think that
a=(select '12345:1' from dual)
is the same than
a = '12345:1'
?

[Updated on: Mon, 17 August 2015 13:13]

Report message to a moderator

Re: Exact String Match in Instr [message #641551 is a reply to message #641550] Mon, 17 August 2015 13:23 Go to previous messageGo to next message
DK555
Messages: 5
Registered: August 2015
Junior Member
But, you, don't you think that
a=(select '12345:1' from dual)
is the same than
a = '12345:1'
?

Both are same. I didn't see what others have commented. I saw others comment only after your silly reply.
Re: Exact String Match in Instr [message #641552 is a reply to message #641551] Mon, 17 August 2015 13:34 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Quote:
I saw others comment only after your silly reply.


So you should read others comments BEFORE you post. Laughing

Re: Exact String Match in Instr [message #641553 is a reply to message #641551] Mon, 17 August 2015 15:05 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3305
Registered: January 2010
Location: Connecticut, USA
Senior Member
DK555 wrote on Mon, 17 August 2015 14:23
Both are same.


They are same result-wise but not execution plan/performance wise. Compare:

SQL> explain plan for
  2  select a, 1 b from tbl where a = '12345:1';

Explained.

SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------
Plan hash value: 2144214008

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     1 |     6 |     3   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| TBL  |     1 |     6 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------

   1 - filter("A"='12345:1')

Note
-----
   - dynamic sampling used for this statement (level=2)

17 rows selected.

SQL> explain plan for
  2  select a, 1 b from tbl where a in (select '12345:1' from dual);

Explained.

SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------
Plan hash value: 3031842229

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     1 |     6 |     5   (0)| 00:00:01 |
|*  1 |  FILTER            |      |       |       |            |          |
|   2 |   TABLE ACCESS FULL| TBL  |     4 |    24 |     3   (0)| 00:00:01 |
|*  3 |   FILTER           |      |       |       |            |          |
|   4 |    FAST DUAL       |      |     1 |       |     2   (0)| 00:00:01 |
---------------------------------------------------------------------------

PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter( EXISTS (SELECT 0 FROM "SYS"."DUAL" "DUAL" WHERE
              :B1='12345:1'))
   3 - filter(:B1='12345:1')

Note
-----
   - dynamic sampling used for this statement (level=2)

22 rows selected.

SQL> 


SY.
Re: Exact String Match in Instr [message #641555 is a reply to message #641553] Mon, 17 August 2015 21:35 Go to previous messageGo to next message
DK555
Messages: 5
Registered: August 2015
Junior Member
Yes I should read them Smile

That's good to know Solomon. But still we need to get the input from the user for a particular string. Because Xandot mentioned that,

As per the string given in the "b" column the output should be displayed. This is what i understood from his question
Re: Exact String Match in Instr [message #641556 is a reply to message #641555] Tue, 18 August 2015 00:30 Go to previous message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Don't trust reach word Xandot post for his question.
@Xandot who should think to feedback and thank (here and in his previous topics) if he wants to be helped again.

Previous Topic: Need a Stored Procedure
Next Topic: Hierarchy flattening and repetition
Goto Forum:
  


Current Time: Sun Aug 10 16:22:43 CDT 2025