How to fix double coding in PostgreSQL?

I have a table in PostgreSQL with words, but some words have invalid UTF-8 characters, such as 0xe7e36fand 0xefbfbd.

How can I identify all the characters inside invalid words and replace them with some kind of character, for example ??

EDIT . My database is in UTF-8, but I think there is a double encoding from other encodings. I think because when I tried to convert one type to LATIN1, I get an error that some char do not exist in this encoding, when I switch to LATIN2, I get the same error, but with a different character.

So what can be done to solve this problem?

+5
source share
1 answer

Using

This solution is for my specific case, but maybe with some changes, it may help other people.

Using

SELECT fix_wrong_encoding('LATIN1');

Functions

-- Convert words with wrong encoding
CREATE OR REPLACE FUNCTION fix_wrong_encoding(encoding_name VARCHAR)
RETURNS VOID
AS $$
DECLARE     
    r RECORD;
    counter INTEGER;
    token_id INTEGER;
BEGIN
    counter = 0;
    FOR r IN SELECT t.id, t.text FROM token t
    LOOP
        BEGIN
            RAISE NOTICE 'Converting %', r.text;
            r.text := convert_from(convert_to(r.text,encoding_name),'UTF8');
            RAISE NOTICE 'Converted to %', r.text;
            RAISE NOTICE 'Checking existence.';
            SELECT id INTO token_id FROM token WHERE text = r.text;             
            IF (token_id IS NOT NULL) THEN
                BEGIN
                    RAISE NOTICE 'Token already exists. Updating ids in textblockhastoken';
                    IF(token_id = r.id) THEN
                        RAISE NOTICE 'Token is the same.';
                        CONTINUE;
                    END IF;
                    UPDATE textblockhastoken SET tokenid = token_id
                    WHERE tokenid = r.id;
                    RAISE NOTICE 'Removing current token.';
                    DELETE FROM token WHERE id = r.id;
                END;
            ELSE
                BEGIN
                    RAISE NOTICE 'Token don''t exists. Updating text in token';
                    UPDATE token SET text = r.text WHERE id = r.id;
                END;
            END IF;
            EXCEPTION WHEN untranslatable_character THEN
                --do nothing
            WHEN character_not_in_repertoire THEN
                --do nothing
            END;
            counter = counter + 1;
            RAISE NOTICE '% token converted', counter;
    END LOOP;
END
$$
LANGUAGE plpgsql;
0
source

All Articles