Validace IČ
Skočit na navigaci
Skočit na vyhledávání
Vycházím z algoritmu publikovaného na http://latrine.dgx.cz/jak-overit-platne-ic-a-rodne-cislo.
CREATE OR REPLACE FUNCTION validate_ic(varchar) RETURNS boolean AS $$ DECLARE s integer; str_parts varchar[]; BEGIN SELECT INTO str_parts regexp_matches FROM regexp_matches($1, E'^(\\d{8})$'); IF FOUND THEN s := (SELECT sum(to_number(substring($1 FROM i FOR 1),'9') * v) % 11 FROM (values(1, 8),(2,7),(3,6), (4,5),(5,4),(6,3), (7,2)) g(i,v)); RETURN CASE WHEN s IN (0,10) THEN substring($1 FROM 8 FOR 1) = '1' WHEN s = 1 THEN substring($1 FROM 8 FOR 1) = '0' ELSE to_number(substring($1 FROM 8 FOR 1), '9') = 11 - s END; ELSE RETURN false; END IF; END; $$ LANGUAGE plpgsql IMMUTABLE STRICT;
Výsledek ověříme:
postgres=# select validate_ic('69663963'); validate_ic ------------- t (1 row)