Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle SQL -- remove partial duplicate from string

Tags:

regex

sql

oracle

I have a table with a column with strings that looke like this:

static-text-here/1abcdefg1abcdefgpxq

From this string 1abcdefg is repeated twice, so I want to remove that partial string, and return:

static-text-here/1abcdefgpxq

I can make no guarantees about the length of the repeat string. In pure SQL, how can this operation be performed?

like image 982
Jeremy Avatar asked Aug 27 '13 19:08

Jeremy


2 Answers

regexp_replace('static-text-here/1abcdefg1abcdefgpxq', '/(.*)\1', '/\1')

fiddle

like image 175
Egor Skriptunoff Avatar answered Nov 04 '22 04:11

Egor Skriptunoff


If you can guarantee a minimum length of the repeated string, something like this would work:

select REGEXP_REPLACE
   (input, 
   '(.{10,})(.*?)\1+', 
   '\1') "Less one repetition" 
   from tablename tn where ...;

I believe this can be expanded to meet your case with some cleverness.

like image 3
user1028435 Avatar answered Nov 04 '22 04:11

user1028435