Обсуждение: Storing/sorting hex data in PostgreSQL

Поиск
Список
Период
Сортировка

Storing/sorting hex data in PostgreSQL

От
JJ
Дата:
I am parsing a map file and storing the hi, mid and low byte values of
symbol addresses in a comma separated value (.csv) file.  I  am then
importing this data into a PostgreSQL database with the copy
command.

The problem is that I want to sort and query the database based on the
hex addresses; from within another program using Npgsql (a .Net data
server for PostgreSQL).

How can I store hex values in PostgreSQL and sort and query them?


Re: Storing/sorting hex data in PostgreSQL

От
nha
Дата:
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.