Created
October 19, 2018 13:10
-
-
Save ryanguill/a87a2249a2d2635f4db20ca0a31cc6a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION pctDisplay (pct numeric, ch text = '|', length int = 20, includeLabel boolean = TRUE) RETURNS text AS $$ | |
DECLARE x TEXT; | |
BEGIN | |
IF length < 1 OR length > 100 THEN | |
length = 20; | |
END IF; | |
IF pct < 0 THEN | |
pct = 0; | |
ELSEIF pct > 100 THEN | |
pct = 100; | |
END IF; | |
SELECT ch || | |
rpad(repeat(ch, ((pct / 100) * length)::int - 2), length - 2) || | |
ch || | |
CASE WHEN includeLabel THEN ' ' || round(pct, 1)::text || '%' ELSE '' END | |
INTO x; | |
RETURN x; | |
END | |
$$ LANGUAGE PLPGSQL; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment