-- -- This one is for text arguments -- drop function bitstring2days(text); create function bitstring2days(text) returns text as ' set result "" foreach bit [split $1 {}] day {S M T W T F S} { if {"$bit" == "1"} { append result $day } else { append result "-" } } return $result ' language pltcl; -- -- This one for if your actual days bits are in an integer -- drop function bitint2days(integer); create function bitint2days(integer) returns text as ' set result "" binary scan [binary format c $1] B8 bits foreach bit [split [string range $bits 1 end] {}] day {S M T W T F S} { if {"$bit" == "1"} { append result $day } else { append result "-" } } return $result ' language pltcl; select bitstring2days('1100111'); select bitint2days(103);