我正在尝试使用一个语句中的所有varchar列中删除所有varchar列中的马车和新行。
我知道我们可以使用以下
之类的东西来为单列执行此操作。select regexp_replace(field, E'[\n\r]+', ' ', 'g' )
在那种情况下,我需要为每一列有一个,除非有任何简单方法,否则我不想这样做。
感谢您的帮助!
您可以执行此功能创建PLPGSQL函数以执行Dynamic SQL,或直接通过DO,作为以下示例(用表"替换my_table
)"):
do $$declare _q text; _table text = '<mytable>';
begin
select 'update '||attrelid::regclass::text||E' setn'||
string_agg(' '||quote_ident(attname)||$q$ = regexp_replace($q$||quote_ident(attname)||$q$, '[nr]+', ' ', 'g')$q$, E',n' order by attnum)
into _q
from pg_attribute
where attnum > 0 and atttypid::regtype::text in ('text', 'varchar')
group by attrelid
having attrelid = _table::regclass;
raise notice E'Executing:nn%', _q;
-- uncomment this line when happy with the query:
-- execute _q;
end;$$;