Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 68215636339 for ; Tue, 21 Jul 2009 08:33:18 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 69955-01-7 for ; Tue, 21 Jul 2009 08:33:01 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtp.insa-lyon.fr (criges14.insa-lyon.fr [134.214.76.242]) by mail.postgresql.org (Postfix) with ESMTP id D3E48636438 for ; Tue, 21 Jul 2009 08:15:36 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by smtp.insa-lyon.fr (Postfix) with ESMTP id EAD94F12B3; Tue, 21 Jul 2009 13:15:31 +0200 (CEST) X-Virus-Scanned: SMTP at INSA-LYON Received: from smtp.insa-lyon.fr ([127.0.0.1]) by localhost (criges14.insa-lyon.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 8hgHwnizVx6c; Tue, 21 Jul 2009 13:15:30 +0200 (CEST) Received: from [127.0.0.1] (liris.univ-lyon1.fr [134.214.142.10]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: nhandrianar) by smtp.insa-lyon.fr (Postfix) with ESMTPSA id 0B0DFF12B2; Tue, 21 Jul 2009 13:15:29 +0200 (CEST) Message-ID: <4A65A31C.7090604@free.fr> Date: Tue, 21 Jul 2009 13:14:36 +0200 From: nha Organization: Lyon, Rhone, France User-Agent: Thunderbird 2.0.0.6 (Macintosh/20070728) MIME-Version: 1.0 To: JJ CC: PgSQL-sql Subject: Re: Storing/sorting hex data in PostgreSQL References: <127c470d-1e29-46fe-87d7-63ce9828fb64@k1g2000yqf.googlegroups.com> In-Reply-To: <127c470d-1e29-46fe-87d7-63ce9828fb64@k1g2000yqf.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200907/90 X-Sequence-Number: 32831 Hello, On 20/07/09 21:59, JJ wrote : > [...] > I want to sort and query the database based on the > hex addresses [...] > How can I store hex values in PostgreSQL and sort and query them? > PostgreSQL (since version 8.3 and maybe older) sets string functions for encoding/decoding strings from/to their hexadecimal representation. The following page presents these functions: http://www.postgresql.org/docs/8.3/interactive/functions-string.html For example: # SELECT decode('6d61702064617461', 'hex'); results in string: map data # SELECT encode('map data', 'hex'); results in hexadecimal representation of "map data": 6d61702064617461 (ie. each character [each byte in fact] is replaced by its hexadecimal representation in ASCII range [space " " is replaced by "20" (hex. value) or "32" (decimal value)]) A (simplier?) alternative would be to convert hexadecimal value into text with the natural class operator: # SELECT '6d61'::text; results in string (text type): 6d61 At this time, I do not know about any PostgreSQL function that converts from/to hex. to/from integer. Such functions may be user-created. The sorting speed may then depend on the meaning of integer values (numeric relation relevance) with your data. Regards. -- nha / Lyon / France.