I am having trouble understanding why this function does not work. I just want to check if a field, all data types, has valid (printable characters).

I am running:
"PostgreSQL 8.2.13 (Greenplum Database 3.3.5.0 build 3) on x86_64-pc-solaris2.10, compiled by GCC gcc.exe (GCC) 4.1.1 compiled on Nov 20 2009 17:32:37"

-- Function: dw.f_containsnonsystemchars(text)

-- DROP FUNCTION dw.f_containsnonsystemchars(text);

CREATE OR REPLACE FUNCTION dw.f_containsnonsystemchars(text)
RETURNS bigint AS
$BODY$
SELECT SUM(checkval) as result
FROM (
SELECT CASE WHEN ($1 ~ '^(-)?[0-9]+$') = 't' THEN 1 ELSE 0 END as checkval
UNION ALL SELECT CASE WHEN $1 IS NULL THEN 1 ELSE 0 END as checkval
UNION ALL SELECT CASE WHEN LENGTH($1) = 0 THEN 1 ELSE 0 END as checkval
) R
$BODY$
LANGUAGE sql VOLATILE;
ALTER FUNCTION dw.f_containsnonsystemchars(text) OWNER TO medwards;