clear markdown compare help best fiddles feedback dbanow.uk
clear markdown donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 2591678 fiddles created (45710 in the last week).

select version();
version
PostgreSQL 12.7 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 8.3.1 20191121 (Red Hat 8.3.1-5), 64-bit
 hidden batch(es)


CREATE OR REPLACE FUNCTION n_divide_ints_and_get_percentage_string(bigint, bigint) RETURNS text LANGUAGE sql IMMUTABLE PARALLEL SAFE AS $function$ SELECT CASE WHEN $2 = 0 THEN 'divide_by_zero' ELSE ROUND($1 * 100 / $2::NUMERIC, 2)::text || '%' END -- explicit cast! $function$
 hidden batch(es)


CREATE OR REPLACE FUNCTION v_divide_ints_and_get_percentage_string(bigint, bigint) RETURNS text LANGUAGE sql IMMUTABLE PARALLEL SAFE AS $function$ SELECT CASE WHEN $2 = 0 THEN 'divide_by_zero' ELSE LEFT(($1 * 100 / $2::FLOAT4)::TEXT, 5) || '%' END -- explicit cast! $function$
 hidden batch(es)


DO $$ BEGIN FOR COUNTER IN 1..100 LOOP PERFORM n_divide_ints_and_get_percentage_string(counter + 100, counter + 1000); END LOOP; END $$;
 hidden batch(es)


DO $$ BEGIN FOR COUNTER IN 1..100 LOOP PERFORM v_divide_ints_and_get_percentage_string(counter + 100, counter + 1000); END LOOP; END $$;
 hidden batch(es)


test_1=# do $$ test_1$# begin test_1$# for counter in 1..10000000 loop test_1$# perform n_divide_ints_and_get_percentage_string(counter + 100, counter + 1000); test_1$# end loop; test_1$# end; $$; DO Time: 31600.193 ms (00:31.600) test_1=#
ERROR: syntax error at or near "test_1" LINE 1: test_1=# do $$ ^
 hidden batch(es)


test_1=# do $$ test_1$# begin test_1$# for counter in 1..10000000 loop test_1$# perform v_divide_ints_and_get_percentage_string(counter + 100, counter + 1000); test_1$# end loop; test_1$# end; $$; DO Time: 31616.354 ms (00:31.616) test_1=#
ERROR: syntax error at or near "test_1" LINE 1: test_1=# do $$ ^
 hidden batch(es)