Question

I have some code record in Oracle DB

for example:

A00105XYZ

CC000036QWE

How to write the criteria, if users input A105XYZ, CC36QWE, these records can still be searched?

Was it helpful?

Solution

You'll probably want to use a regular expression:

SELECT STR, REGEXP_REPLACE(STR,'([^[:digit:]]*)(0*)(.*)','\1\3') NEW_STR 
FROM 
(SELECT 'A00105XYZ' STR FROM DUAL UNION
 SELECT 'CC000036QWE' STR FROM DUAL UNION
 SELECT 'FD403T' STR FROM DUAL UNION
 SELECT '000000010' STR FROM DUAL)


╔═════════════╦═════════╗
║     STR     ║ NEW_STR ║
╠═════════════╬═════════╣
║ 000000010   ║ 10      ║
║ A00105XYZ   ║ A105XYZ ║
║ CC000036QWE ║ CC36QWE ║
║ FD403T      ║ FD403T  ║
╚═════════════╩═════════╝

OTHER TIPS

One way would be to replace the 0s on both sides:

where replace(code, '0', '') = replace (:var, '0', '')

Note that A00105XYZ will also match A15XYZ.

Look at this as an example:

with tbl as
 (select 'A00105XYZ' as str
    from dual
  union all
  select 'CC000036QWE'
    from dual)
select str, utl_match.edit_distance('A105XYZ', str) chars_off
  from tbl
 where utl_match.edit_distance('A105XYZ', str) <= 4

This finds the correct row of the 2 row test table (the 2 examples you gave). The reason is because it was less than or equal to 4 characters off (it's 2 characters off). (4 or fewer characters would have to be changed for it to be an exact match)

Test with 'CC36QWE', it also works. (it's 4 characters off)

You can change the threshold of 4 to whatever you want, as needed, to increase or decrease the fuzziness of the search.

Edit - may want to use edit distance similarity instead as it will better account for varied string lengths (it's normalized 0 to 1), for more info see http://docs.oracle.com/cd/E11882_01/appdev.112/e25788/u_match.htm#ARPLS71217

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top