I am trying to store a bytea as a value in hstore and I keep getting the following error:
function hstore(unknown, bytea) does not exist
Here is what I have tried:
UPDATE users set store = store || hstore('key1', pgp_pub_encrypt('testval',dearmor('xxxx')));
am trying to store a bytea as a value in hstore
You cannot safely store bytea in hstore without some kind of encoding, because hstore is stored as text in the current text encoding, but bytea has no text encoding and may also contain null bytes that are not legal in text strings.
So you need to encode the bytea as hex, base64, or some other form that makes it into valid text in the current encoding.
The simplest way is to cast it to text, but I recommend instead using encode and decode to/from base64. This encoding is more compact than the text or hex encodings used for bytea's text representation in PostgreSQL, and it's also independent of the value of the bytea_output setting.
e.g.
UPDATE users
SET store = store
|| hstore('key1', encode( pgp_pub_encrypt('testval',dearmor('xxxx'))), 'base64')
WHERE ... ;
There are two variants of the hstore() function taking arrays, one taking a row / record and one taking two text values. The last one is for you:
SELECT hstore('foo','abc'::bytea::text); -- cast bytea to text!
Your attempt failed the rules of function type resolution because there is no explicit cast registered from bytea to text. Postgres falls back to input / output conversion for that, which does not count for function type resolution:
Is there a way to disable function overloading in Postgres
So:
UPDATE users
SET store = store
|| hstore('key1', pgp_pub_encrypt('testval',dearmor('xxxx'))::text)
WHERE ... ;
Related
I have a postgresql table profile with columns (username, friends). username is a string and friends is a json type and has the current values ["Mary", "Bob]. I want to append an item at the end of the array so it is ["Mary", "Bob", "Alice"]
I am currently have tried:
UPDATE profile SET friends = friends || '["Alice"]'::jsonb WHERE username = 'David';
This yields the error:
[ERROR] 23:56:23 error: operator does not exist: json || jsonb
I tried changing the first expression to include json instead of jsonb but I then got the error:
[ERROR] 00:06:25 error: operator does not exist: json || json
Other answers seem to suggesst the || operator is indeed a thing, e.g.:
Appending (pushing) and removing from a JSON array in PostgreSQL 9.5+
How do I append an item to the end of a json array?
The data type json is much more limited than jsonb. Use jsonb instead of json and the solution you found simply works.
While stuck with json, a possible workaround is to cast to jsonb and back:
UPDATE profile
SET friends = (friends::jsonb || '["Alice"]')::json
WHERE username = 'David';
You might use an explicit type for jsonb '["Alice"]', too, but that's optional. While the expression is unambiguous like that, the untyped string literal will be coerced to jsonb automatically. If you instead provide a typed value in place of '["Alice"]', the explicit cast may be required.
If friends can be NULL, you'll want to define what should happen then. Currently, nothing happens. Or, to be precise, an update that doesn't change the NULL value.
Then again, if you only have simple arrays of strings, consider the plain Postgres array type text[] instead.
I'm trying to get a hash of a decimal value and convert it to an integer. But the query results in the following error:
Numeric value 'b902cc4550838229a710bfec4c38cbc7eb11082367a409df9135e7f007a96bda' is not recognized
SELECT (CAST(sha2(TO_VARCHAR(ABS(12.5)), 256) AS INTEGER) % 100) AS temp_value
What is the correct way to convert a hash string to an integer in Snowflake?
I can not use any user defined functions. And have to go with Snowflake native functions.
The hash value contains alphabetic character so it will throw an error
SELECT --(CAST(
sha2(
TO_VARCHAR(
ABS(12.5)), 256)-- AS INTEGER) % 100)
AS temp_value;
You need to convert the hex value from the hash encoding to be int.
I've not been able to find a function built into Snowflake that does this, but if you have a look in the following link, it will explain how to create a javascript function to do the conversion for you:
https://snowflakecommunity.force.com/s/article/faq-does-snowflake-have-a-hex-to-int-type-function
If you use the function in the link, then your code becomes something like this:
SELECT (CAST(js_hextoint(sha2(TO_VARCHAR(ABS(12.5)), 256)) AS INTEGER) % 100) AS temp_value
I've not been able to test the above code I'm afraid, so there may be a bracket in the wrong place...
You have a 56 digit hexadecimal number. It's not going to fit into the maximum numeric precision of 38. You could use a floating point number, but that will lose precision.
create or replace function CONV(VALUE_IN string, OLD_BASE float, NEW_BASE float)
returns string
language javascript
as
$$
// Usage note: Loses precision for very large inputs
return parseInt(VALUE_IN, Math.floor(OLD_BASE).toString(Math.floor(NEW_BASE)));
$$;
select conv('b902cc4550838229a710bfec4c38cbc7eb11082367a409df9135e7f007a96bda', 16, 10);
--Returns 8.368282050700398e+76
For hex to integer, check Does snowflake have a ‘hex’ to ‘int’ type native function?. My guess is that most people checking this question (1k views) are looking for that.
But this specific question wants to convert a sha2 digest to integer for comparison purposes. My advice for that specific question is "don't".
That's because the hex string in the question represent the integer 83682820507003986697271120393377917644380327831689630185856829040117055843290, which is too much to handle even by Java's BigInteger.
Instead, just compare strings/binary to check if the values match or not.
Requirement:
To convert SRT_MMASTER table's MESSAGE_DATA field data into readable string format or in internal table.
I have tried different function modules to convert Byte String (Blob) data stored in SRT_MMASTER table's MESSAGE_DATA field but none of it returned readable string format or at least generate an XML file in return.
I have tried Function modules like :
SCMS_BINARY_TO_STRING,
SDIXML_XML_TO_DOM,
SDIXML_DOM_TO_DATA,
SMUM_XML_PARSE
and methods like :
cl_soap_xml_parser=>get_data
cl_soap_xml_parser=>get_formatted_data
CALL METHOD cl_bcs_convert=>raw_to_string
and more, but none were able to convert it to correct readable format.
Can you suggest which function module or class/method can be used to solve the purpose?
You can use IMPORT from DATA BUFFER and cl_soap_xml_helper=>xstring_to_string
DATA:
lt_message_item TYPE srt_persistency_item_t,
lx_message_data TYPE xstring.
SELECT SINGLE message_data FROM srt_mmaster INTO lx_message_data.
IMPORT message_data = lt_message_item FROM DATA BUFFER lx_message_data.
DATA(lv_string) = cl_soap_xml_helper=>xstring_to_string( lt_item[ 1 ]-value ).
BigQuery Standard SQL documentation suggests that BYTE fields can be coerced into STRINGS.
We have a byte field that is the result of SHA256 hashing a field using BigQuery itself.
We now want to coerce it to a STRING, yet when we run "CAST(field_name to STRING)" we get an error:
Query Failed Error: Invalid cast of bytes to UTF8 string
What is preventing us from getting a string from this byte field? Is it surmountable? If so, what is the solution?
Below example should show you an idea
#standardSQL
WITH t AS (
SELECT SHA256('abc') x
)
SELECT x, TO_BASE64(x)
FROM t
in short - you can use TO_BASE64() for this
If you want to see the "traditional" representation of the hash in String, you have to use TO_HEX() function.
WITH table AS (
SELECT SHA256('abc') as bytes_field
)
SELECT bytes_field, TO_HEX(bytes_field) as string_field
FROM table
By default in the UI, BigQuery shows you the base64 representation but if you want to compare it with other sha256 function from other language, for example, you have to use TO_HEX()
You can try SAFE_CONVERT_BYTES_TO_STRING() function.
reference: SAFE_CONVERT_BYTES_TO_STRING
I have a table in oracle db with data type RAW.
I would like export into an xml file but when I retrieve the data from the RAW column I get a [#4r5... instead of the value in db (123454678...)
How can I write this value in xml (the goal is to export in another db)
Thanks a lot.
You don't say what code you're using to access the column, but it looks like you are getting back a byte[]
You should be able to print the actual bytes in groovy using:
println obj.toList().join(',')
But if you were expecting a string or something, you'll need to know how it was converted to bytes in the first place...
If you want the byte array converted to an array of integers, you can just do:
List byteList = a.toList()
(or if you want your bytes to be treated as unsigned, you can do)
List byteList = a.collect { it & 0xff }
It depends what you intend to do with the array though... It is probably best left as an array...
Did you try PL/SQL?
declare
a mytable.rawcol%TYPE;
begin
select rawcol into a from mytable;
-- now do something with "a"
end;