WITH
  FUNCTION babbage(p_ziel IN varchar2, p_max INTEGER) RETURN varchar2 IS
    v_max    INTEGER:= greatest(p_max,to_number('1E+'||LENGTH(to_char(p_ziel))));
    v_start  NUMBER:= CASE WHEN substr(p_ziel,1,1)='0' THEN CEIL(SQRT('1'||p_ziel)) ELSE CEIL(SQRT(p_ziel)) END;
    v_length NUMBER:= to_number('1E+'||LENGTH(to_char(p_ziel)));   
  BEGIN
    -- first check
    IF substr(p_ziel,-1) IN (2,3,7,8) THEN 
      RETURN 'The exact square of an integer cannot end with '''||substr(p_ziel,-1)||''', so there is no such smallest number whose square ends in '''||p_ziel||'''';
    END IF; 
    -- second check
    IF regexp_count(p_ziel,'([^0]0{1,}$)')=1 AND MOD(regexp_count(regexp_substr(p_ziel,'(0{1,}$)'),'0'),2)=1 THEN
      RETURN 'An exact square of an integer cannot end with an odd number of zeros, so there is no such smallest number whose square ends in '''||p_ziel||'''';
    END IF; 
    -- main cycle
    while v_start < v_max loop
      exit WHEN MOD(v_start**2,v_length) = p_ziel;   
      v_start:= v_start + 1; 
    END loop;
    -- output 
    IF v_start = v_max THEN
      RETURN 'There is no such smallest number before '||v_max||' whose square ends in '''||p_ziel||'''';
    ELSE 
      RETURN ''||v_start||' is the smallest number, whose square '||regexp_replace(to_char(v_start**2),'(\d{1,})('||p_ziel||')','\1''\2''')||' ends in '''||p_ziel||''''; 
    END IF; 
    --
  END;

--Test
SELECT babbage('222',100000) AS res FROM dual
UNION ALL 
SELECT babbage('33',100000) AS res FROM dual
UNION ALL 
SELECT babbage('17',100000) AS res FROM dual
UNION ALL 
SELECT babbage('888',100000) AS res FROM dual
UNION ALL 
SELECT babbage('1000',100000) AS res FROM dual
UNION ALL 
SELECT babbage('000',100000) AS res FROM dual
UNION ALL 
SELECT babbage('269696',100000) AS res FROM dual -- strict Babbage Problem
UNION ALL 
SELECT babbage('269696',10) AS res FROM dual
UNION ALL 
SELECT babbage('169696',10) AS res FROM dual
UNION ALL
SELECT babbage('19696',100000) AS res FROM dual
UNION ALL 
SELECT babbage('04',100000) AS res FROM dual;