# s should be a bitstring. # Right-pad s with 0s so that the length of the output string is a multiple of 8. CREATE OR REPLACE FUNCTION print_bits(s) as ( format('{:0<{}}', s, (ceil(length(s) / 8) * 8)::INT) ); # Example: select print_bits('101'::BITSTRING);