Re: Postgres Wishlist - Mailing list pgsql-novice

From Donald Kerr
Subject Re: Postgres Wishlist
Date
Msg-id 4F5F71B9C0264C48AA2A82D50FCCE366@DELLM4500
Whole thread Raw
In response to Re: Postgres Wishlist  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: Postgres Wishlist
List pgsql-novice
Thank you, Tom.

I have tried what you suggest but it does not seem to work:
'x'||substring(col,3,2)::text::bit(8)::int returns - "9" is not a valid
binary digit.

Test SQL below:
SELECT col, 'x'||substring(col,3,2)::text::bit(8)::int AS Test,
x'00'::integer || ' ' || x'99'::integer || ' ' ||  x'FF'::integer AS oscolor
FROM cartographictext WHERE COL <> '000000' LIMIT 10

Many thanks.

Regards,

Donald


-----Original Message-----
From: Tom Lane [mailto:tgl@sss.pgh.pa.us]
Sent: 12 November 2010 21:38
To: Donald Kerr
Cc: 'Steve Crawford'; pgsql-novice@postgresql.org
Subject: Re: [NOVICE] Postgres Wishlist


"Donald Kerr" <donald.kerr@dkerr.co.uk> writes:
> I am trying to convert from hex to decimal and can do that
> successfully using the following code:

> SELECT x'FF'::integer;

> which outputs 255 and is exactly what I want.

What you're doing there is abusing the bit-string-literal syntax. I think
you could get access to the same behavior for a non-constant input like
this:

    SELECT 'xff'::text::bit(8)::int;

or in practice

    SELECT ('x' || some-string-variable)::bit(8)::int;

This is relying on some undocumented behavior of the bit-type input
converter, but I see no reason to expect that would break.  A possibly
bigger issue is that it requires PG >= 8.3 since there wasn't a text to bit
cast before that.

> Thew data colum contains html color codes like "0099FF" and I want to
> convert these to, in this case, "0 153 255".

It seems very unlikely that you're going to get any built-in solution to
that.  You don't have just a hex-to-decimal conversion problem; you also
have the problems of splitting the given string into three pieces and
deciding how you're going to represent the three-component result of the
conversion.  And both of your choices there seem pretty arbitrary/
specialized.

I think your best bet would be to write a custom function that does what you
want.  This'd likely be a one-liner in plperl, for example.  You could do it
in plpgsql if you don't want to rely on plperl being installed, but it'd be
a tad more tedious.

            regards, tom lane


pgsql-novice by date:

Previous
From: LazyTrek
Date:
Subject: Re: login error - Warning: pg_connect(): Unable to connect to PostgreSQL server: FATAL: password authentication failed for user "kamik"
Next
From: Michael Glaesemann
Date:
Subject: Re: Postgres Wishlist