Re: SELECT question - Mailing list pgsql-general

From Alvaro Herrera
Subject Re: SELECT question
Date
Msg-id 20031104213404.GB12410@dcc.uchile.cl
Whole thread Raw
In response to SELECT question  (Alex <alex@meerkatsoft.com>)
List pgsql-general
On Tue, Nov 04, 2003 at 07:54:54PM +0900, Alex wrote:

> I have a bit string , 7 bits, every bit representing a day of the week.
> e.g. 1110011.
> Is there and easy way where I can translate/format that string in a query.
> I want to give the string back with a '-' for every 0 and the first char
> of the Day for every '1'.
> example 1100111 = SM--TFS.

A simple perl function:

sub mybits {
    my $bits = shift;
    my $ret = "";
    foreach my $i (0 .. 6) {
        substr($ret, $i, 1,
                (substr($bits, $i, 1) =~ /1/ ?
                 (qw(S M T W T F S))[$i] : "-"));
    }
    return $ret;
}

$ ./test.pl 1001011
S--W-FS

You can of course use it with plperl.

--
Alvaro Herrera (<alvherre[a]dcc.uchile.cl>)
Hi! I'm a .signature virus!
cp me into your .signature file to help me spread!

pgsql-general by date:

Previous
From: Stephan Szabo
Date:
Subject: Re: Using SUBSELECT in CHECK expressions
Next
From: Tom Lane
Date:
Subject: Re: INSERT performance