From: Ben Ostrowsky Date: Mon, 12 Apr 2010 21:54:27 +0000 (+0000) Subject: Added logic to attempt_phone for handling a few more variants correctly X-Git-Url: http://git.equinoxoli.org/?p=migration-tools.git;a=commitdiff_plain;h=444fb02d9b8af869163dbad49eb31a5fa6ea42b0 Added logic to attempt_phone for handling a few more variants correctly --- diff --git a/sql/base/base.sql b/sql/base/base.sql index 59eec2f..5137ef2 100644 --- a/sql/base/base.sql +++ b/sql/base/base.sql @@ -697,16 +697,20 @@ CREATE OR REPLACE FUNCTION migration_tools.attempt_phone (TEXT,TEXT) RETURNS TEX DECLARE phone TEXT := $1; areacode TEXT := $2; + temp TEXT := ''; output TEXT := ''; n_digits INTEGER := 0; BEGIN - output := REGEXP_REPLACE(phone, '[^0-9]*([0-9]{3})[^0-9]*([0-9]{3})[^0-9]*([0-9]{4})', E'\\1-\\2-\\3'); - n_digits := LENGTH(REGEXP_REPLACE(output, '[^0-9]', '', 'g')); + temp := REGEXP_REPLACE(phone, '^1[^0-9]*', ''); + temp := REGEXP_REPLACE(temp, '[^0-9]*([0-9]{3})[^0-9]*([0-9]{3})[^0-9]*([0-9]{4})', E'\\1-\\2-\\3'); + n_digits := LENGTH(REGEXP_REPLACE(temp, '[^0-9]', '', 'g')); IF n_digits = 7 THEN - RETURN (areacode || '-' || output); + temp := REGEXP_REPLACE(temp, '[^0-9]*([0-9]{3})[^0-9]*([0-9]{4})', E'\\1-\\2'); + output := (areacode || '-' || temp); ELSE - RETURN output; + output := temp; END IF; + RETURN output; END; $$ LANGUAGE PLPGSQL STRICT VOLATILE;