Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to express a Postgres literal int in hexadecimal?

Tags:

I have a big list of hexadecimal numbers I'd like to insert into a PostgresQL table. I tried something like this:

INSERT INTO foo (i) VALUES (0x1234); 

...but that didn't work. Is this possible?

like image 240
mike Avatar asked Jan 05 '09 21:01

mike


People also ask

What is Smallint in PostgreSQL?

PostgreSQL allows a type of integer type namely SMALLINT . It requires 2 bytes of storage size and can store integers in the range of -37, 767 to 32, 767. It comes in handy for storing data like the age of people, the number of pages in a book, etc. Syntax: variable_name SMALLINT.

What is serial4 in PostgreSQL?

serial (or serial4) creates integer columns. bigserial (or serial8) creates bigint columns. smallserial (or serial2) creates smallint columns.


2 Answers

As you've noted, you can start with a bit-string constant written in hexadecimal, and then type-cast it to the type you want. So,

INSERT INTO foo (i) VALUES (CAST(x'1234' AS int)) 

or

INSERT INTO foo (i) VALUES (x'1234'::int) -- postgres-specific syntax 
like image 122
Rob Kennedy Avatar answered Oct 13 '22 22:10

Rob Kennedy


This seems to work:

 CAST(X'3e000000' AS INT) 
like image 27
mike Avatar answered Oct 14 '22 00:10

mike