-- Contribuído por Paulo Martins (paulo.uabmail@gmail.com) -- IBAN case when substr(IBAN, 1, 4) <> 'PT50' then '** IBAN estrangeiro! **' when length(trim(substr(IBAN, 5))) < 21 then '** IBAN Errado! **' when int(substr(IBAN, 24, 2)) = (98 - mod( 73*int(substr(IBAN, 5, 1)) + 17*int(substr(IBAN, 6, 1)) + 89*int(substr(IBAN, 7, 1)) + 38*int(substr(IBAN, 8, 1)) + 62*int(substr(IBAN, 9, 1)) + 45*int(substr(IBAN, 10, 1)) + 53*int(substr(IBAN, 11, 1)) + 15*int(substr(IBAN, 12, 1)) + 50*int(substr(IBAN, 13, 1)) + 05*int(substr(IBAN, 14, 1)) + 49*int(substr(IBAN, 15, 1)) + 34*int(substr(IBAN, 16, 1)) + 81*int(substr(IBAN, 17, 1)) + 76*int(substr(IBAN, 18, 1)) + 27*int(substr(IBAN, 19, 1)) + 90*int(substr(IBAN, 20, 1)) + 09*int(substr(IBAN, 21, 1)) + 30*int(substr(IBAN, 22, 1)) + 03*int(substr(IBAN, 23, 1)), 97)) then 'Ok' else '** IBAN Errado! **' end -- NIF case when length(trim(NIF))=9 and substr(trim(NIF), 1, 1) between '1' and '9' and substr(trim(NIF), 2, 1) between '0' and '9' and substr(trim(NIF), 3, 1) between '0' and '9' and substr(trim(NIF), 4, 1) between '0' and '9' and substr(trim(NIF), 5, 1) between '0' and '9' and substr(trim(NIF), 6, 1) between '0' and '9' and substr(trim(NIF), 7, 1) between '0' and '9' and substr(trim(NIF), 8, 1) between '0' and '9' and substr(trim(NIF), 9, 1) between '0' and '9' then (case when (11-mod(int(substr(trim(NIF), 1, 1))*9 + int(substr(trim(NIF), 2, 1))*8 + int(substr(trim(NIF), 3, 1))*7 + int(substr(trim(NIF), 4, 1))*6 + int(substr(trim(NIF), 5, 1))*5 + int(substr(trim(NIF), 6, 1))*4 + int(substr(trim(NIF), 7, 1))*3 + int(substr(trim(NIF), 8, 1))*2, 11)) >= 10 and substr(trim(NIF), 9, 1)='0' then 'Ok' when char(11-mod(int(substr(trim(NIF), 1, 1))*9 + int(substr(trim(NIF), 2, 1))*8 + int(substr(trim(NIF), 3, 1))*7 + int(substr(trim(NIF), 4, 1))*6 + int(substr(trim(NIF), 5, 1))*5 + int(substr(trim(NIF), 6, 1))*4 + int(substr(trim(NIF), 7, 1))*3 + int(substr(trim(NIF), 8, 1))*2 , 11)) = substr(trim(NIF), 9, 1) then 'Ok' else 'BAD!' end) else 'BAD!' end) <> 'Ok'