convert integer to varbinary
phil2
✭
Hello
Is it possible to convert int value directly into binary data type? For now I figured out that I can cast int to hex string and then convert it to binary like this HEX_TO_BINARY(LPAD(TO_HEX(i & 0xffffffff), 8, '0'))
You can say that there is no need for binary as all binary operators available for integer as well. And I would say that I need varbinary to combine many integer features togeather in a binary hash like this binary_feature1 || binary_feature2 || binary_feature3 || ...
So I'm wondering if there is something more efficient than TO_HEX -> LPAD -> HEX_TO_BINARY to convert int to binary
0
Comments
Hi Phil2 -
If you want to combine several integer based features into one variable:
Could you not do it just using bit operations on integers?
Let's say that:
SQL in general does not provide the possibility of using base-2 numeric literals, unfortunately.
But I work around that by using bitwise operations.
For the constants, I create a view:
Then, I combine the features by or-ing them together:
And then, you can get the individual features back as Booleans like so:
All you'd have to do is to CROSS JOIN your queries with that one-row view ...
Just a suggestion ...
Marco
Hello, Marco.
I have too many features and int8 is not enough for me. Currently I need 16 bytes to store them. And there going to be more features. So I dont wont to split them
For now I wrote scalar UDF to perform int to binary cast. And it works much faster than string manipulations
@phil2 - Do you mind sharing your UDX with the community?
Yep, I can do this
I finally got some free time and published some of my code.
Here we go: https://github.com/phil-88/vertica-udf/blob/master/README.md
@phil2 - Thanks for sharing your code with the community!!!
SELECT int2bin(2);