SQL pattern matching

 ̄綄美尐妖づ 提交于 2019-12-05 10:17:54

You didn't say what version of Oracle you are using. This example is based on 11g version. You can use edit_distance function of utl_match package to determine how many characters you need to change in order to turn one string to another. greatest function returns the greatest value in the list of passed in parameters. Here is an example:

-- sample of data 
with t1(col1, col2) as(
  select 'This is my first assignment in SQL', 'My first assignment in SQL ' from dual
)
-- the query
select trunc(((greatest(length(col1), length(col2)) -  
              (utl_match.edit_distance(col2, col1))) * 100) / 
             greatest(length(col1), length(col2)), 2) as "%"
  from t1

result:

         %
----------
     70.58

Addendum

As @jonearles correctly pointed out, it is much simpler to use edit_distance_similarity function of utl_match package.

 with t1(col1, col2) as(
     select 'This is my first assignment in SQL', 'My first assignment in SQL ' from dual
  )
  select utl_match.edit_distance_similarity(col1, col2) as "%"
    from t1
   ;

Result:

         %
----------
        71
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!