473,386 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

SELECT question

Hi,

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.

thanks for any suggestions
Alex



---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 12 '05 #1
4 1875
On Tuesday 04 November 2003 10:54, Alex wrote:
Hi,

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.


You probably want a small function in plpgsql - see the procedural languages
section of the manual for details. You might want to check the cookbook at
http://techdocs.postgresql.org/ and see if there's similar code you can use
as inspiration.

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 12 '05 #2


On Tue, 4 Nov 2003, Alex wrote:
Hi,

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.


You could write a Postgres function to do this, depending on your
programming skills, but you did ask for a query based solution.

An SQL based approach could use a series of SQL's to substring the
1010101 into separate attributes, then update each accordingly & join
them back into a single attribute afterward. A bit more cumbersome but
for those with SQL capabilities but weak on programming this is pretty
straightforward. Wrap the whole lot in a shell script for ease of use & a
one off run. Not elegant but for a one off it should suffice.

As shown below....
Cheers,

Brent Wood

eg: (off the top of my head- this approach should work OK as a script,
tho you may need to tweak the syntax & fit your attributes into the
commands)
/bin/sh

#select data into new table with day of week as separate attrs
psql -d <db> -c "select into table temp_days
attr1,
attr2,
substring(days_of_week, 1,1) as 'sun',
substring(days_of_week, 2,1) as 'mon',
...
;"

# update each day depending on 0 or 1, sun shown as example
psql -d <db> -c "update temp_days
set sun 'S' where sun = '1';"

psql -d <db> -c "update temp_days
set sun '-' where sun = '0';"

.....

# concat all the days back into a single attribute
psql -d <db> -c "select into table new_table
attr1,
attr2,
sun || mon || .... as days_of_week,
...
;"

#finally drop the old table (once you are happy with the result)
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 12 '05 #3
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!

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #4
Brent Wood wrote:

On Tue, 4 Nov 2003, Alex wrote:
Hi,

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.


You could write a Postgres function to do this, depending on your
programming skills, but you did ask for a query based solution.


See attachment
Jan

--
#================================================= =====================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me. #
#================================================= = Ja******@Yahoo.com #

--
-- 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);
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mohammed Mazid | last post by:
Hi folks! Can anyone please help me with this? I am developing a Quiz program but I am stuck with "multiple answers". Basically I need some sort of code that would select multiple answers...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
2
by: marco | last post by:
Dear List, as it seems, MS SQL as used in Access does not allow a select INTO within a UNION query. Also, it seems that a UNION query can not be used as a subquery. Maybe my (simplified)...
6
by: @sh | last post by:
Need help again! Here's the head script... ---------------------------------------------------------------------------------------- function Ash_ChangePicFromSelect(TheSource,TheDestination) {...
1
by: Christopher DeMarco | last post by:
Hi all... I've written a class to provide an interface to popen; I've included the actual select() loop below. I'm finding that "sometimes" popen'd processes take "a really long time" to...
5
by: GTi | last post by:
Whats wring with this code? <select class=EditField size="1" name="PlantUnitID" title="Select line"> <option value="0" >Standalone Unit</option> <option value="1" selected >Connected Unit...
2
by: Eitan | last post by:
Hello, I want a solutions for a compicateds sql select statments. The selects can use anything : views, stored procedures, analytic functions, etc... (not use materialized view, unless it is...
21
beacon
by: beacon | last post by:
Hello to everybody, I have a section on a form that has 10 questions, numbered 1-10, with 3 option buttons per question. Each of the option buttons have the same response (Yes, No, Don't know),...
17
by: trose178 | last post by:
Good day all, I am working on a multi-select list box for a standard question checklist database and I am running into a syntax error in the code that I cannot seem to correct. I will also note...
25
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if my question needs to be here or in coldfusion. If i have my question is in the wrong section i am sorry in advance an will move it to the correct section. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.