Skip to content

Instantly share code, notes, and snippets.

@Jonathan-49
Last active February 3, 2025 07:45
Show Gist options
  • Save Jonathan-49/a62a9b4a1ebef57b39e68a3349db706c to your computer and use it in GitHub Desktop.
Save Jonathan-49/a62a9b4a1ebef57b39e68a3349db706c to your computer and use it in GitHub Desktop.
Cover text SQL UDF
-- UDF that covers, hides, obscures characters, either from the left or right.
-- Parameters: P_INPUT - input text
-- P_Char - Replacement character
-- P_Num - number of characters to cover, if negative, then
-- characters in the text are replaced from the right.
-- If postive then characters are replaced from the left.
-- Returns - input text with characters replaced from the left or right.
-- IBM i Db2
Create or replace Function Cover (
P_INPUT VARChar(1000), P_Char Char(1), P_NUM INT)
Returns VARCHAR(1000)
Language SQL
Deterministic
Reads SQL Data
Called on NULL Input
Disallow Parallel
Begin
Declare v_output varchar(1000);
Declare v_length INT;
set V_length =Length(trim(P_Input));
If abs(P_NUM)>V_Length then
set V_output = repeat(P_Char, V_Length);
Elseif P_NUM<0 then set
V_output = substr(trim(P_Input), 1, V_length-abs(P_Num)) concat repeat(P_Char, abs(P_Num));
Else set
V_output = repeat(P_Char, P_Num) concat substr(trim(P_Input), P_NUM+1, V_length-P_Num) ;
End If;
Return V_output;
End ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment