postgresql中加入不可见字符,并清除

postgresql中加入不可见字符,并清除

create table if not exists test_table(name character varying(10), id character varying(10));

delete from test_table;

INSERT INTO test_table VALUES ('aaa', '000');
INSERT INTO test_table VALUES ('aaax', '001');
INSERT INTO test_table VALUES (chr(1)||chr(2)||'aaa', '002');

select length(name), name, id from test_table order by length(name) ASC


删除不可见字符

-- 函数说明:将表test_table中所有的不可见字符替换掉
create or replace function delete_unvisiable_char() returns character varying as $$
DECLARE
    row RECORD;      
BEGIN

-- 查询表中所有类型为字符串的列
FOR row in select column_name from information_schema.columns where table_name = 'test_table' and data_type like 'character%' LOOP   
FOR i IN 1..31 LOOP --替换所有的不可见字符为空格(除了chr(0)之外)
        EXECUTE  'update test_table set ' || row.column_name || ' = replace(' || row.column_name || ', chr(' || i || '), '''' )';
    END LOOP;           
END LOOP;

RETURN 1;

END;
$$ LANGUAGE plpgsql;

select * from delete_unvisiable_char()



猜你喜欢

转载自blog.csdn.net/zlf19910726/article/details/80077436