postgresql - Postgres - Replace special character # with some text -


i have function imports data csv file - in turn creates table based on no of columns in csv file.

there 1 line code imports columns names csv file , renames temporary column name actual names per data in csv file.

format('select unnest(string_to_array(lower(replace(trim(insert_from_csv::text, ''()''),'' '',''_'')), '','')) insert_from_csv col_1 = %l', col_first) , format('alter table insert_from_csv rename column col_%s %s', iter, col); 

in first statement used 'replace' remove spaces within '_', when try same '#' gets error -b'ze in postgres # refers comments.

does knows ways replace '#' 'some text'.

function reference:

create or replace function public.load_csv_file ( target_table text, csv_path text, col_count integer )  returns void $$  declare  iter integer; -- dummy integer iterate columns col text; -- variable keep column name @ each iteration col_first text; -- first column name, e.g., top left corner on csv file or     spreadsheet  begin set schema 'public';  create table insert_from_csv ();  -- add enough number of columns iter in 1..col_count loop     execute format('alter table insert_from_csv add column col_%s text;', iter); end loop;  -- copy data csv file execute format('copy insert_from_csv %l delimiter '','' quote ''"'' csv ', csv_path);  iter := 1; col_first := (select col_1 insert_from_csv limit 1);  -- update column names based on first row has column names col in execute format('select unnest(string_to_array(lower(replace(trim(insert_from_csv::text, ''()''),'' '',''_'')), '','')) insert_from_csv col_1 = %l', col_first) loop     execute format('alter table insert_from_csv rename column col_%s %s', iter, col);     iter := iter + 1; end loop;  -- delete columns row execute format('delete insert_from_csv %s = %l', col_first, col_first);  -- change temp table name name given parameter, if not blank if length(target_table) > 0     execute format('alter table insert_from_csv rename %i', target_table); end if;  end;  $$ language plpgsql; 

csv data:

date    metrics # of applications 01-apr-17       95 02-apr-17       122 03-apr-17       110 04-apr-17       94 05-apr-17       122 06-apr-17       93 07-apr-17       97 

thanks


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -