SET PATH *LIBL ; 

CREATE OR REPLACE FUNCTION CISTOOLS.SPLIT_STRING ( 
	IN_STRING VARCHAR(32676) , 
	IN_DELIMITOR VARCHAR(32) ) 
	RETURNS TABLE ( 
	ROW_NUM INTEGER , 
	FRAGMENT VARCHAR(32676) )   
	LANGUAGE SQL 
	SPECIFIC CISTOOLS.SPLIT_CHAR 
	NOT DETERMINISTIC 
	READS SQL DATA 
	CALLED ON NULL INPUT 
	PROGRAM NAME SPLIT_CHAR 
	CONCURRENT ACCESS RESOLUTION DEFAULT 
	SET OPTION  ALWBLK = *ALLREAD , 
	ALWCPYDTA = *OPTIMIZE , 
	COMMIT = *NONE , 
	DECRESULT = (31, 31, 00) , 
	DYNDFTCOL = *NO , 
	DYNUSRPRF = *USER , 
	SQLCURRULE = *STD , 
	SRTSEQ = *HEX   
	BEGIN 
RETURN 
WITH RECURSIVE CHUNKS ( IDX , START_POS , NEXT_POS ) AS ( 
-- base case 
SELECT * 
FROM TABLE ( 
VALUES ( 1 , 1 , LOCATE_IN_STRING ( 
IN_STRING , IN_DELIMITOR , 1 ) ) 
) X ( IDX , START_POS , NEXT_POS ) 
UNION ALL 
-- recursive case 
SELECT IDX + 1 , NEXT_POS + LENGTH ( IN_DELIMITOR ) , 
CASE 
WHEN 
NEXT_POS + LENGTH ( IN_DELIMITOR ) <= LENGTH ( 
IN_STRING ) 
THEN 
LOCATE_IN_STRING ( 
IN_STRING , IN_DELIMITOR , NEXT_POS + 
LENGTH ( IN_DELIMITOR ) ) 
ELSE 0 
END 
FROM CHUNKS 
WHERE NEXT_POS > 0 
AND NEXT_POS + LENGTH ( IN_DELIMITOR ) <= LENGTH ( 
IN_STRING ) 
) SELECT IDX , 
SUBSTR ( 
IN_STRING , START_POS , 
CASE 
WHEN 
NEXT_POS = 0 
THEN LENGTH ( IN_STRING ) - START_POS + 1 
ELSE NEXT_POS - START_POS 
END ) AS FRAGMENT 
FROM CHUNKS ; 
END  ; 

GRANT ALTER , EXECUTE   
ON SPECIFIC FUNCTION CISTOOLS.SPLIT_CHAR 
TO AMAPICS WITH GRANT OPTION ; 

GRANT EXECUTE   
ON SPECIFIC FUNCTION CISTOOLS.SPLIT_CHAR 
TO PUBLIC ; 


SET PATH *LIBL ; 

CREATE OR REPLACE FUNCTION CISTOOLS.SPLIT_STRING ( 
	IN_STRING VARCHAR(32676) , 
	IN_LEN INTEGER ) 
	RETURNS TABLE ( 
	ROW_NUM INTEGER , 
	FRAGMENT VARCHAR(32676) )   
	LANGUAGE SQL 
	SPECIFIC CISTOOLS.SPLIT_LEN 
	NOT DETERMINISTIC 
	READS SQL DATA 
	CALLED ON NULL INPUT 
	PROGRAM NAME SPLIT_LEN 
	CONCURRENT ACCESS RESOLUTION DEFAULT 
	SET OPTION  ALWBLK = *ALLREAD , 
	ALWCPYDTA = *OPTIMIZE , 
	COMMIT = *NONE , 
	DECRESULT = (31, 31, 00) , 
	DYNDFTCOL = *NO , 
	DYNUSRPRF = *USER , 
	SQLCURRULE = *STD , 
	SRTSEQ = *HEX   
	BEGIN 
RETURN SELECT RN , SUBSTR ( IN_STRING , RN , IN_LEN ) 
FROM TABLE ( 
CISTOOLS . ROWS_COUNTER ( LENGTH ( IN_STRING ) ) 
) AS X 
WHERE MOD ( ( RN - 1 ) , IN_LEN ) = 0 ; 
END  ; 

GRANT ALTER , EXECUTE   
ON SPECIFIC FUNCTION CISTOOLS.SPLIT_LEN 
TO AMAPICS WITH GRANT OPTION ; 

GRANT EXECUTE   
ON SPECIFIC FUNCTION CISTOOLS.SPLIT_LEN 
TO PUBLIC ; 

