Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You only have 128 possible values for your packed day strings (seven slots and two possible values for each slot means 128 possibilities) so just generate a table that covers all the options and join to that table to get your unpacked string. You should be able to generate a table like this pretty easily:</p> <pre><code>packed | unpacked ----------+----------- '0000000' | '' '0000001' | 'Saturday' ... '1000001' | 'Sunday, Saturday' ... </code></pre> <p>Then you can join your packed day column to this table on <code>packed</code> and select <code>unpacked</code> to get the human friendly string. Keep in mind that a function over a finite domain <strong>is</strong> an association table and if the domain is small then you can quite easily implement the function as a table.</p> <hr> <p>If you must do it the hard way, then this will work in 8.1 but it is rather horrendous and should convince you that (1) you shouldn't be storing your days like that and (b) you should be handling this sort of formatting outside the database. I wouldn't do anything like this in real life, I'm only including it because I wanted to see if I could come up with something that would work in the constrained 8.1 environment. You might want to upgrade ASAP too, 8.1 is pretty long in the tooth and no longer supported.</p> <p>First you want a table of day names to join against:</p> <pre><code>create table days (num int not null, name varchar(9) not null); insert into days (num, name) values (1, 'Sunday'); insert into days (num, name) values (2, 'Monday'); insert into days (num, name) values (3, 'Tuesday'); insert into days (num, name) values (4, 'Wednesday'); insert into days (num, name) values (5, 'Thursday'); insert into days (num, name) values (6, 'Friday'); insert into days (num, name) values (7, 'Saturday'); </code></pre> <p>Then a custom aggregate for joining strings together separated by commas:</p> <pre><code>create function comma_join(t1 text, t2 text) returns text as $$ begin if t1 is null or t2 is null then return null; elseif t1 = '' or t2 = '' then return t1 || t2; end if; return t1 || ', ' || t2; end; $$ language plpgsql; create aggregate group_comma_join( sfunc = comma_join, basetype = text, stype = text, initcond = '' ); </code></pre> <p>And finally, an unpacking function to hide the all the ugliness:</p> <pre><code>create function unpack_days(days_string text) returns text as $$ declare s text; begin select group_comma_join(name) from ( select name into s from days d join generate_series(1, 7) n(num) on d.num = n.num where substr(days_string, n.num, 1) = '1' ) dt; return s; end $$ language plpgsql; </code></pre> <p>Now you can say this:</p> <pre><code>=&gt; select unpack_days('0111110'); unpack_days ---------------------------------------------- Monday, Tuesday, Wednesday, Thursday, Friday (1 row) =&gt; select unpack_days('0000000'); unpack_days ------------- (1 row) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload