Skip to content

Instantly share code, notes, and snippets.

@ryanguill
Created October 19, 2018 13:10
Show Gist options
  • Save ryanguill/a87a2249a2d2635f4db20ca0a31cc6a4 to your computer and use it in GitHub Desktop.
Save ryanguill/a87a2249a2d2635f4db20ca0a31cc6a4 to your computer and use it in GitHub Desktop.
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