Re: Postgres Wishlist

From: "Donald Kerr" <donald(dot)kerr(at)dkerr(dot)co(dot)uk>
To: "'Tom Lane'" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "'Steve Crawford'" <scrawford(at)pinpointresearch(dot)com>, <pgsql-novice(at)postgresql(dot)org>
Subject: Re: Postgres Wishlist
Date: 2010-11-13 08:32:51
Message-ID: 4F5F71B9C0264C48AA2A82D50FCCE366@DELLM4500
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)sss(dot)pgh(dot)pa(dot)us]
Sent: 12 November 2010 21:38
To: Donald Kerr
Cc: 'Steve Crawford'; pgsql-novice(at)postgresql(dot)org
Subject: Re: [NOVICE] Postgres Wishlist

"Donald Kerr" <donald(dot)kerr(at)dkerr(dot)co(dot)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

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Michael Glaesemann 2010-11-13 08:44:53 Re: Postgres Wishlist
Previous Message LazyTrek 2010-11-12 22:40:20 Re: login error - Warning: pg_connect(): Unable to connect to PostgreSQL server: FATAL: password authentication failed for user "kamik"