migration_tools.name_parse_out_last_first_middle_and_random_affix for LAST FIRST...
[migration-tools.git] / sql / base / base.sql
index 19a48f8..8ea837a 100644 (file)
@@ -352,6 +352,72 @@ CREATE OR REPLACE FUNCTION migration_tools.name_parse_out_last_comma_prefix_firs
     END;
 $$ LANGUAGE PLPGSQL STRICT IMMUTABLE;
 
+CREATE OR REPLACE FUNCTION migration_tools.name_parse_out_last_first_middle_and_random_affix (TEXT) RETURNS TEXT[] AS $$
+    DECLARE
+        full_name TEXT := $1;
+        temp TEXT;
+        family_name TEXT := '';
+        first_given_name TEXT := '';
+        second_given_name TEXT := '';
+        suffix TEXT := '';
+        prefix TEXT := '';
+    BEGIN
+        temp := full_name;
+        -- Use values, not structure, for prefix/suffix, unless we come up with a better idea
+        IF temp ilike '%MR.%' THEN
+            prefix := 'Mr.';
+            temp := REGEXP_REPLACE( temp, E'MR\.\\s*', '', 'i' );
+        END IF;
+        IF temp ilike '%MRS.%' THEN
+            prefix := 'Mrs.';
+            temp := REGEXP_REPLACE( temp, E'MRS\.\\s*', '', 'i' );
+        END IF;
+        IF temp ilike '%MS.%' THEN
+            prefix := 'Ms.';
+            temp := REGEXP_REPLACE( temp, E'MS\.\\s*', '', 'i' );
+        END IF;
+        IF temp ilike '%DR.%' THEN
+            prefix := 'Dr.';
+            temp := REGEXP_REPLACE( temp, E'DR\.\\s*', '', 'i' );
+        END IF;
+        IF temp ilike '%JR%' THEN
+            suffix := 'Jr.';
+            temp := REGEXP_REPLACE( temp, E'JR\.?\\s*', '', 'i' );
+        END IF;
+        IF temp ilike '%JR,%' THEN
+            suffix := 'Jr.';
+            temp := REGEXP_REPLACE( temp, E'JR,\\s*', ',', 'i' );
+        END IF;
+        IF temp ilike '%SR%' THEN
+            suffix := 'Sr.';
+            temp := REGEXP_REPLACE( temp, E'SR\.?\\s*', '', 'i' );
+        END IF;
+        IF temp ilike '%SR,%' THEN
+            suffix := 'Sr.';
+            temp := REGEXP_REPLACE( temp, E'SR,\\s*', ',', 'i' );
+        END IF;
+        IF temp ilike '%III%' THEN
+            suffix := 'III';
+            temp := REGEXP_REPLACE( temp, E'III', '', 'i' );
+        END IF;
+        IF temp ilike '%II%' THEN
+            suffix := 'II';
+            temp := REGEXP_REPLACE( temp, E'II', '', 'i' );
+        END IF;
+        IF temp ilike '%IV%' THEN
+            suffix := 'IV';
+            temp := REGEXP_REPLACE( temp, E'IV', '', 'i' );
+        END IF;
+
+        family_name := BTRIM( REGEXP_REPLACE(temp,E'^(\\S+).*$',E'\\1') );
+        first_given_name := BTRIM( REGEXP_REPLACE(temp,E'^\\S+\\s+(\\S+).*$',E'\\1') );
+        second_given_name := BTRIM( REGEXP_REPLACE(temp,E'^\\S+\\s+\\S+\\s*(.*)$',E'\\1') );
+
+        RETURN ARRAY[ family_name, prefix, first_given_name, second_given_name, suffix ];
+    END;
+$$ LANGUAGE PLPGSQL STRICT IMMUTABLE;
+
+
 CREATE OR REPLACE FUNCTION migration_tools.address_parse_out_citystatezip (TEXT) RETURNS TEXT[] AS $$
     DECLARE
         city_state_zip TEXT := $1;