SELECT ifnull(i00.pno, '') as pno, 
       tabl01.fpno as fpno, 
       tabl01.f01p01 as  f01p01,
       tabl01.f01p02 as  f01p02,
       tabl01.f01p03 as  f01p03
       
FROM 
(
SELECT f01.p02-1 as fr1,
       f01.p02 + f01.p03 -1 as len,        
       f01.p01 as f01p01, 
       f01.p02 as f01p02, 
       f01.p03 as f01p03,
       if(f01.p02<=1, '', 
       substr(
       concat(  ifnull(i21.pc01, ''), 
                ifnull(i22.pc01, ''), 
                ifnull(i29.pc01, ''), 
                ifnull(i30.pc01, ''), 
                space(14)), 1, f01.p02-1 ) ) as fpno
FROM f01
LEFT JOIN i21 ON i21.nr01={peg_arr[1]}
LEFT JOIN i22 ON i22.nr01={peg_arr[2]}
LEFT JOIN i29 ON i29.nr01={peg_arr[3]}
LEFT JOIN i30 ON i30.nr01={peg_arr[4]}
) as tabl01
LEFT JOIN i00 ON substr(i00.pno, 1, tabl01.fr1)=tabl01.fpno
              && length(i00.pno)=tabl01.len
              && !(substr(i00.pno, tabl01.f01p02, tabl01.f01p03) REGEXP '[^0-9]') 
ORDER BY i00.pno DESC 
LIMIT 1;