Skip to content
Snippets Groups Projects
city_cleaning_pg_spalte_in_text_.txt 1.33 KiB
Newer Older
  • Learn to ignore specific revisions
  • Christopher Lenke's avatar
    Christopher Lenke committed
    -- DROP FUNCTION schema_xyz.city_cleaning_pg(text);
    
    CREATE OR REPLACE FUNCTION schema_xyz.city_cleaning_pg(spalte_in text)
     RETURNS character varying
     LANGUAGE plpgsql
    AS $function$
    
    DECLARE
    
    
    ergebnis varchar;
    city_cleaned varchar;
    
    
    
    BEGIN
    
       
    city_cleaned:=regexp_replace(upper(spalte_in),'D?E?O?W?-? ?[0-9]{4,6} ?',' ' , 'g');
    city_cleaned:=regexp_replace(city_cleaned,'D?E?O?W?-? ?[0-9]{2,2} [0-9]{3,3} ?',' ' , 'g');
    city_cleaned:=regexp_replace(city_cleaned,'D?E?o?W?-? ?[0-9]{3,3} [0-9]{2,2} ?',' ' , 'g');
    city_cleaned:=ltrim(rtrim(city_cleaned));
    city_cleaned:=replace(city_cleaned,', ', ' ');
    city_cleaned:=replace(city_cleaned,'  ', ' ');
    city_cleaned:=replace(city_cleaned,'  ', ' ');
    
    if (city_cleaned like '%,')
    then
    city_cleaned:=substr(city_cleaned, 1, length(city_cleaned)-1);
    end if;
    
    city_cleaned:=ltrim(rtrim(city_cleaned));
    city_cleaned:=regexp_replace(city_cleaned,'^[0-9]{1,3} ',' ' , 'g');
    city_cleaned:=regexp_replace(city_cleaned,' [0-9]{1,3}$',' ' , 'g');
    city_cleaned:=regexp_replace(city_cleaned,' [0-9]{1,3} ',' ' , 'g');
    city_cleaned:=replace(city_cleaned,', ', ' ');
    city_cleaned:=replace(city_cleaned,'  ', ' ');
    city_cleaned:=replace(city_cleaned,'  ', ' ');
    city_cleaned:=ltrim(rtrim(city_cleaned));
    
    
     ergebnis:=city_cleaned;
       
       
       RETURN ergebnis;
    
    	END;
    $function$
    ;