Created
June 1, 2018 09:49
-
-
Save jef-sure/9d06888f727bc30f8b0f199baf5d418e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CREATE FUNCTION inc5 (num NUMERIC) | |
| returns NUMERIC | |
| language SQL | |
| AS $$ | |
| WITH recursive n5 (i, l, nt, ln, c) AS ( | |
| SELECT -1, LENGTH(num::text)+1, num::text, '', 1 | |
| UNION ALL | |
| SELECT i+1, l - 1, nt, | |
| CASE (substring(nt FROM l-1 FOR 1)::INT + c) | |
| WHEN 5 | |
| THEN '0' | |
| ELSE (substring(nt FROM l-1 FOR 1)::INT + c)::text | |
| END, | |
| CASE (substring(nt FROM l-1 FOR 1)::INT + c) | |
| WHEN 5 | |
| THEN 1 | |
| ELSE 0 | |
| END | |
| FROM n5 | |
| WHERE l > 1 | |
| ) | |
| SELECT string_agg(digit, '')::NUMERIC | |
| FROM (SELECT ln AS digit FROM n5 WHERE i >= 0 ORDER BY i DESC) ch; | |
| $$; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment