In my Postgres 13 DB I want to update a column mother_tongues_id
in my table based on name columns.
Names are like "Le Minh Thien" and I want to check if those contain words like 'le' or 'lee' then set nationality to 'Vietnam'. The words check table is some thing like this:
language | noun |
---|---|
vietnam | +lee+le+long+la+ |
chinese | +lu+zhu+ |
Here is my UPDATE
code:
UPDATE resume
SET mother_tongues_id = (
SELECT id FROM mother_tongues
WHERE check_language(resume.id) = id
limit 1);
WHERE mother_tongues_id IS NULL;
Here is my check_language()
function:
CREATE OR REPLACE FUNCTION check_language(rid integer) RETURNS integer AS
$BODY$
DECLARE i int = (SELECT (LENGTH(CONCAT(first_name, ' ',last_name)) - LENGTH(replace(CONCAT(first_name, ' ',last_name), ' ', ''))) FROM resume where id = rid);
DECLARE sname varchar = '';
DECLARE sid int = 0
LOOP
SET i = i - 1;
SET sname = SELECT split_part(SELECT CONCAT(first_name, ' ',last_name) FROM resume where id = rid, ' ', i)
if select id from mother_tongues WHERE noun ILIKE '%+sname+%' then SET sid = id;
end if;
IF i = 1 THEN
EXIT;
END IF;
END LOOP;
if sid > 0 then RETURN sid;
else RETURN NULL;
end if;
END;
$BODY$
LANGUAGE plpgsql;
I try to create a function in Postgres with loop to split the name and check the words, however always error.