Skip to content

Instantly share code, notes, and snippets.

@pokoli
Created January 23, 2014 09:20
Show Gist options
  • Select an option

  • Save pokoli/8575512 to your computer and use it in GitHub Desktop.

Select an option

Save pokoli/8575512 to your computer and use it in GitHub Desktop.
PostgreSQL Function to calculate IBAN the given a Spanish Acount Number
CREATE OR REPLACE FUNCTION calculate_iban(account varchar)
RETURNS varchar AS $$
DECLARE num numeric;
DECLARE DC varchar(2);
BEGIN
SELECT CAST(replace(account, ' ', '')|| '142800' AS numeric) INTO num;
SELECT LPAD(CAST(98 - MOD(num, 97) AS varchar(2)), 2, '0') INTO DC;
RETURN 'ES' || DC || replace(account, ' ', '');
END;
$$ LANGUAGE plpgsql;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment