Guest User

Untitled

a guest
Dec 14th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. create or replace function ibge_dv(codigo char(6))
  2. returns char(1) as $$
  3.  
  4. select ((10 - resto) * (resto != 0)::int)::char(1)
  5. from (
  6. select
  7. (
  8. a135 +
  9. left(a2, 1)::int + right(a2, 1)::int +
  10. left(a4, 1)::int + right(a4, 1)::int +
  11. left(a6, 1)::int + right(a6, 1)::int
  12. ) % 10 as resto
  13. from (
  14. select
  15. a[1] + a[3] + a[5] as a135,
  16. to_char(a[2] * 2, 'FM09') as a2,
  17. to_char(a[4] * 2, 'FM09') as a4,
  18. to_char(a[6] * 2, 'FM09') as a6
  19. from (
  20. select string_to_array(codigo, null)::integer[] as a
  21. ) s
  22. ) s
  23. ) s
  24. ;
  25.  
  26. $$ language sql;
Add Comment
Please, Sign In to add comment