Advertisement
Guest User

Untitled

a guest
Jan 29th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 1.06 KB | None | 0 0
  1. CREATE FUNCTION `REGEXP_SEARCH_SMALLEST_TERM`(`stringSubject` MEDIUMTEXT, `stringPattern` TEXT)
  2.     RETURNS TEXT
  3.     LANGUAGE SQL
  4.     DETERMINISTIC
  5.     NO SQL
  6.     SQL SECURITY DEFINER
  7.     COMMENT ''
  8. BEGIN
  9.     DECLARE searchSubject TEXT DEFAULT stringSubject;
  10.     DECLARE searchSmallestContent TEXT DEFAULT NULL;
  11.     DECLARE searchSmallestContentLength INT DEFAULT LENGTH(stringSubject);
  12.     DECLARE matchLast TEXT DEFAULT NULL;
  13.     DECLARE matchPosition INT DEFAULT NULL;
  14.     DECLARE matchLength INT DEFAULT NULL;
  15.    
  16.     defaultLooping:
  17.     LOOP
  18.         SET matchLast = REGEXP_SUBSTR(searchSubject, stringPattern);
  19.        
  20.         IF matchLast = '' THEN
  21.             LEAVE defaultLooping;
  22.         END IF;
  23.        
  24.         SET matchPosition = REGEXP_INSTR(searchSubject, stringPattern);
  25.         SET matchLength   = LENGTH(matchLast);
  26.         SET searchSubject = SUBSTRING(searchSubject, matchPosition + matchLength);
  27.                
  28.         IF matchLength < searchSmallestContentLength THEN
  29.             SET searchSmallestContent = matchLast;
  30.             SET searchSmallestContentLength = matchLength;
  31.         END IF;
  32.        
  33.         ITERATE defaultLooping;
  34.     END LOOP;
  35.    
  36.     RETURN searchSmallestContent;
  37. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement