SET PATH *LIBL ; 

CREATE OR REPLACE FUNCTION CISTOOLS.SANITIZE_COLUMN_NAME ( 
	INPUT_STRING VARCHAR(32000) ) 
	RETURNS VARCHAR(32000)   
	LANGUAGE SQL 
	SPECIFIC CISTOOLS.SANITIZE_COLUMN_NAME 
	NOT DETERMINISTIC 
	READS SQL DATA 
	CALLED ON NULL INPUT 
	CONCURRENT ACCESS RESOLUTION DEFAULT 
	SET OPTION  ALWBLK = *ALLREAD , 
	ALWCPYDTA = *OPTIMIZE , 
	COMMIT = *NONE , 
	DECRESULT = (31, 31, 05) , 
	DYNDFTCOL = *NO , 
	DYNUSRPRF = *USER , 
	SQLCURRULE = *STD , 
	SRTSEQ = *HEX   
	BEGIN 
DECLARE OUTPUT_STRING VARCHAR ( 32000 ) DEFAULT ' ' ; 
SET OUTPUT_STRING = REGEXP_REPLACE ( 
TRIM ( 
REGEXP_REPLACE ( 
CAST ( INPUT_STRING AS VARCHAR ( 32000 ) CCSID 37 ) , 
'[\s\-.,;''"`\[\]()/\\|!*\%_+=<>^$~#?@:]' , ' ' ) ) , 
'\s+' , '_' ) ; 
IF SUBSTR ( OUTPUT_STRING , 1 , 1 ) BETWEEN '0' AND '9' THEN 
SET OUTPUT_STRING = '"' || OUTPUT_STRING || '"' ; 
END IF ; 
RETURN TRIM ( OUTPUT_STRING ) ; 
END  ; 

COMMENT ON PARAMETER SPECIFIC FUNCTION CISTOOLS.SANITIZE_COLUMN_NAME 
( INPUT_STRING IS 'Input String' ) ; 

LABEL ON SPECIFIC FUNCTION CISTOOLS.SANITIZE_COLUMN_NAME 
	IS 'Sanitize Column Name' ; 

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

GRANT EXECUTE   
ON SPECIFIC FUNCTION CISTOOLS.SANITIZE_COLUMN_NAME 
TO PUBLIC ; 

