Solved

REPLACE in a WHERE

Posted on 2014-04-25
5
245 Views
Last Modified: 2014-04-30
Problem:
On the S_FIRST_NAME and S_LAST_NAME of table columns TABLE_CONTACT being saved in some cases two spaces between the names, for example:
S_FIRST_NAME = 'JOHAN  Stive'
S_FIRST_NAME = 'JOHAN Stive'
The query that is installed on production does not bring the right information, see query 2 and query 3

Query 2 (11 rows)
SELECT TC.OBJID, TC.PHONE, TC.X_CONTACT_ADD2ADDRESS, TC.S_LAST_NAME, TC.S_FIRST_NAME, TC.X_CONTACT_STATUS, TC.X_DOC_TYPE, X_DOC_NUM,
       trim(SUBSTR(TS.X_CATEGORY_CUSTOMER, 1, 8))
  FROM SA.TABLE_CONTACT TC, SA.TABLE_CONTACT_ROLE CR, TABLE_SITE TS
 WHERE CR.CONTACT_ROLE2CONTACT = TC.OBJID
   AND CR.CONTACT_ROLE2SITE = TS.OBJID
   AND TC.S_FIRST_NAME LIKE 'JOHAN STIVE%'
   AND TC.S_LAST_NAME LIKE 'TOLENTINO RAMIREZ%'
   AND trim(SUBSTR(TS.X_CATEGORY_CUSTOMER, 1, 8)) = 'Postpago'

Query 3 (1 rows)
SELECT TC.OBJID, TC.PHONE, TC.X_CONTACT_ADD2ADDRESS, TC.S_LAST_NAME, TC.S_FIRST_NAME, TC.X_CONTACT_STATUS, TC.X_DOC_TYPE, X_DOC_NUM,
       trim(SUBSTR(TS.X_CATEGORY_CUSTOMER, 1, 8))
  FROM SA.TABLE_CONTACT TC, SA.TABLE_CONTACT_ROLE CR, TABLE_SITE TS
 WHERE CR.CONTACT_ROLE2CONTACT = TC.OBJID
   AND CR.CONTACT_ROLE2SITE = TS.OBJID
   AND TC.S_FIRST_NAME LIKE 'JOHAN  STIVE%'
   AND TC.S_LAST_NAME LIKE 'TOLENTINO RAMIREZ%'
   AND trim(SUBSTR(TS.X_CATEGORY_CUSTOMER, 1, 8)) = 'Postpago'

Solution: The only one I've found is the query 1 with the use of REPLACE, but the problem is using a TABLE ACCESS FULL, there is some other way. The DBA will not allow the use of replace that causes a performance problem
query 1 (12 rows)
SELECT TC.OBJID, TC.PHONE, TC.X_CONTACT_ADD2ADDRESS, TC.S_LAST_NAME, TC.S_FIRST_NAME, TC.X_CONTACT_STATUS, TC.X_DOC_TYPE, X_DOC_NUM,
       trim(SUBSTR(TS.X_CATEGORY_CUSTOMER, 1, 8))
  FROM SA.TABLE_CONTACT TC, SA.TABLE_CONTACT_ROLE CR, TABLE_SITE TS
 WHERE CR.CONTACT_ROLE2CONTACT = TC.OBJID
   AND CR.CONTACT_ROLE2SITE = TS.OBJID
   AND REPLACE(TC.S_FIRST_NAME, '  ', ' ') = 'JOHAN STIVE'
   AND REPLACE(TC.S_LAST_NAME, '  ', ' ') = 'TOLENTINO RAMIREZ'
   AND trim(SUBSTR(TS.X_CATEGORY_CUSTOMER, 1, 8)) = 'Postpago'
query-1.jpg
query-2.jpg
query-3.jpg
Problema-REPLACE.docx
0
Comment
Question by:enrique_aeo
  • 2
  • 2
5 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 40023149
if the REPLACE results in a FULL table scan, that's because the current index which includes the fields would need to include the same replace function call instead of the field itself.
this is called a function-based index.

you could also try:

 AND TC.S_FIRST_NAME LIKE 'JOHAN%STIVE%'
which translates into:
 AND TC.S_FIRST_NAME LIKE REPLACE( TRIM('JOHAN  STIVE%') , ' ', '%' )

the trim is to avoid that users putting leading spaces would result in a LIKE '%....%'

hope this helps
0
 
LVL 16

Expert Comment

by:Wasim Akram Shaik
ID: 40023462
Yes as Hengel suggests ,the best option would be to create a functional index on your column

however in case there are restrictions to do so, you can use your existing query itself and and add an instr function to the existing statement like this,

SELECT TC.OBJID, TC.PHONE, TC.X_CONTACT_ADD2ADDRESS, TC.S_LAST_NAME, TC.S_FIRST_NAME, TC.X_CONTACT_STATUS, TC.X_DOC_TYPE, X_DOC_NUM,
       trim(SUBSTR(TS.X_CATEGORY_CUSTOMER, 1, 8))
  FROM SA.TABLE_CONTACT TC, SA.TABLE_CONTACT_ROLE CR, TABLE_SITE TS
 WHERE CR.CONTACT_ROLE2CONTACT = TC.OBJID
   AND CR.CONTACT_ROLE2SITE = TS.OBJID
   AND TC.S_FIRST_NAME LIKE 'JOHAN%'
   AND TC.S_LAST_NAME LIKE 'TOLENTINO%'
   AND trim(SUBSTR(TS.X_CATEGORY_CUSTOMER, 1, 8)) = 'Postpago'
and instr(TC.S_FIRST_NAME,' ',1,2)>0
and instr(TC.S_LAST_NAME,' ',1,2)>0

amongst the existing results filter the results which have the second occurrence of space in any of the columns
0
 

Author Comment

by:enrique_aeo
ID: 40026176
This query it's work
SELECT TC.OBJID,
       TC.PHONE,
       TC.X_CONTACT_ADD2ADDRESS,
       TC.S_LAST_NAME,
       TC.S_FIRST_NAME,
       TC.X_CONTACT_STATUS,
       TC.X_DOC_TYPE,
       X_DOC_NUM,
       trim(SUBSTR(TS.X_CATEGORY_CUSTOMER, 1, 8))
  FROM TABLE_CONTACT TC, TABLE_CONTACT_ROLE CR, TABLE_SITE TS
 WHERE CR.CONTACT_ROLE2CONTACT = TC.OBJID
   AND CR.CONTACT_ROLE2SITE = TS.OBJID
   AND TC.S_FIRST_NAME LIKE REPLACE(TRIM('JOHAN STIVE%'), ' ', '%')
      AND TC.S_LAST_NAME LIKE REPLACE(TRIM('TOLENTINO RAMIREZ%'), ' ', '%')
   AND trim(SUBSTR(TS.X_CATEGORY_CUSTOMER, 1, 8)) = 'Postpago'

-- 2 rows
JOHAN  STIVE
JOHAN STIVE

this query it's not work
SELECT TC.OBJID, TC.PHONE, TC.X_CONTACT_ADD2ADDRESS, TC.S_LAST_NAME, TC.S_FIRST_NAME, TC.X_CONTACT_STATUS, TC.X_DOC_TYPE, X_DOC_NUM,
       trim(SUBSTR(TS.X_CATEGORY_CUSTOMER, 1, 8))
  FROM TABLE_CONTACT TC, TABLE_CONTACT_ROLE CR, TABLE_SITE TS
 WHERE CR.CONTACT_ROLE2CONTACT = TC.OBJID
   AND CR.CONTACT_ROLE2SITE = TS.OBJID
   AND TC.S_FIRST_NAME LIKE 'JOHAN%'
   AND TC.S_LAST_NAME LIKE 'TOLENTINO%'
   AND trim(SUBSTR(TS.X_CATEGORY_CUSTOMER, 1, 8)) = 'Postpago'
and instr(TC.S_FIRST_NAME,' ',1,2)>0
and instr(TC.S_LAST_NAME,' ',1,2)>0
-- 0 rows
0
 
LVL 16

Expert Comment

by:Wasim Akram Shaik
ID: 40026609
its because of AND operator ,try this.. it should work

SELECT TC.OBJID, TC.PHONE, TC.X_CONTACT_ADD2ADDRESS, TC.S_LAST_NAME, TC.S_FIRST_NAME, TC.X_CONTACT_STATUS, TC.X_DOC_TYPE, X_DOC_NUM,
       trim(SUBSTR(TS.X_CATEGORY_CUSTOMER, 1, 8))
  FROM TABLE_CONTACT TC, TABLE_CONTACT_ROLE CR, TABLE_SITE TS
 WHERE CR.CONTACT_ROLE2CONTACT = TC.OBJID
   AND CR.CONTACT_ROLE2SITE = TS.OBJID
   AND TC.S_FIRST_NAME LIKE 'JOHAN%'
   AND TC.S_LAST_NAME LIKE 'TOLENTINO%'
   AND trim(SUBSTR(TS.X_CATEGORY_CUSTOMER, 1, 8)) = 'Postpago'
and( instr(TC.S_FIRST_NAME,' ',1,2)>0
or instr(TC.S_LAST_NAME,' ',1,2)>0)
0
 

Author Comment

by:enrique_aeo
ID: 40033392
show only one rows
JOHAN  STIVE
and use TABLE ACCESS FULL
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now