Converting large values to/from hex

Moderator: NorbertKrupa

Post Reply
aschneider
Newbie
Newbie
Posts: 3
Joined: Thu Aug 15, 2013 5:32 pm

Converting large values to/from hex

Post by aschneider » Fri Oct 25, 2013 10:10 pm

The following behavior is frustrating to me:

Code: Select all

select 0x4318F7A9055EA43237BE62E130338B33C7D6AD43;
                     ?column?                     
--------------------------------------------------
 383059173779444683676001354605068116670933282115
(1 row)

select HEX_TO_INTEGER('4318F7A9055EA43237BE62E130338B33C7D6AD43');
ERROR 5408:  Value "0x4318F7A9055EA43237BE62E130338B33C7D6AD43" is out of range for type numeric(37,15)

select TO_HEX(383059173779444683676001354605068116670933282115);
ERROR 3457:  Function TO_HEX(numeric) does not exist, or permission is denied for TO_HEX(numeric)
In other words, Vertica can convert hex literals into numerics, but the SQL functions to do this only work up to a certain size. Am I doing something wrong, or is there a better way to convert large integers to/from hex?

User avatar
JimKnicely
Site Admin
Site Admin
Posts: 1825
Joined: Sat Jan 21, 2012 4:58 am
Contact:

Re: Converting large values to/from hex

Post by JimKnicely » Fri Oct 25, 2013 11:59 pm

Hi,

The largest integer data type in Vertica is INT8 which stores up to 64-bit values. So the largest value would be 9,223,372,036,854,775,808.

Your HEX value of "4318F7A9055EA43237BE62E130338B33C7D6AD43" is 383,059,173,779,444,683,676,001,354,605,068,116,670,933,282,115 in decimal! That's a bit bigger :shock: than what Vertica can handle.

Note: I used the site http://www.mathsisfun.com/binary-decima ... erter.html to convert your hex to decimal.

You can convert the large HEX to a bit string...

Code: Select all

dbadmin=> SELECT TO_BITSTRING(HEX_TO_BINARY('4318F7A9055EA43237BE62E130338B33C7D6AD43'));
                                                                           TO_BITSTRING
------------------------------------------------------------------------------------------------------------------------------------------------------------------
 0100001100011000111101111010100100000101010111101010010000110010001101111011111001100010111000010011000000110011100010110011001111000111110101101010110101000011
(1 row)
But that's probably not going to help you...
Jim Knicely

Image

Note: I work for Vertica. My views, opinions, and thoughts expressed here do not represent those of my employer.

aschneider
Newbie
Newbie
Posts: 3
Joined: Thu Aug 15, 2013 5:32 pm

Re: Converting large values to/from hex

Post by aschneider » Sun Oct 27, 2013 10:47 pm

Ah, that makes sense - I'm starting to understand the errors a bit better. So what I need is a function that converts hex to the unbounded NUMERIC type rather than INTEGER?

Post Reply

Return to “Vertica SQL”