Click here to Skip to main content
15,901,771 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What data type should I use for Source column?
I want to import data into postgreSQL DB

data in source column is
0x00
0x00
0x01
0xBA
0x90

Thanks.

What I have tried:

I tried using bytea datatype. when I import data into DB
I am getting folloowing data into source column
<binary data>
<binary data>
<binary data>
<binary data>
<binary data>
Posted
Updated 15-Jun-17 7:20am
v2
Comments
RedDk 15-Jun-17 12:17pm    
Ok, here's the COMMENT control. "Found it!" ...
RickZeeland 15-Jun-17 13:01pm    
Can you tell us what datatype the source database field has ?

1 solution

The 0x prefix means that it is a hexadecimal number, you can store it as an integer.
To get the hexadecimal notation back you can use the PostgreSQL function to_hex().
More info about hexadecimal here: Hexadecimal - Wikipedia[^]
In C# you can convert hex to integer as follows:
C#
Convert.ToInt32("BA", 16)
In PostgreSQL you can use:
SQL
select ('x'||lpad(the_hex_value,16,'0'))::bit(64)::bigint;
Or this function:
SQL
CREATE OR REPLACE FUNCTION hex_to_int(hexval varchar) RETURNS integer AS $$
DECLARE
    result  int;
BEGIN
    EXECUTE 'SELECT x''' || hexval || '''::int' INTO result;
    RETURN result;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT;
see: sql - Convert hex string to bigint in postgres - Stack Overflow[^]
postgresql - Convert hex in text representation to decimal number - Stack Overflow[^]
 
Share this answer
 
v3
Comments
peoria123 15-Jun-17 16:25pm    
Thanks..Yes it is in hex..I tried using inter datatype too but it is giving datatype error.
CPallini 16-Jun-17 3:01am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900