Using binary data¶
The usefulness and flexibility of the BINARY data type is best demonstrated by example. This topic provides practical examples of tasks that involve the BINARY data type and its three encoding schemes.
Converting between hex and base64¶
The BINARY data type can be used as an intermediate step when converting between hex and base64 strings.
Convert from hex to base64 using TO_CHAR:
Convert from base64 to hex:
Converting between text and UTF-8 bytes¶
Strings in Snowflake are composed of Unicode characters, while binary values are composed of bytes. By converting a string to a binary value with the UTF-8 format, you can directly manipulate the bytes that make up the Unicode characters.
Convert single-character strings to their UTF-8 representation in bytes using TO_BINARY:
Convert a UTF-8 byte sequence to a string using TO_CHAR , TO_VARCHAR:
Getting the MD5 digest in base64¶
Convert the binary MD5 digest to a base64 string using TO_CHAR:
Convert to binary with variable format¶
Convert strings to binary values using a binary format extracted from the string. The statement includes the TRY_TO_BINARY and SPLIT_PART functions:
Try multiple formats for the conversion:
Note
Since the above queries use TRY_TO_BINARY, the result is NULL if the format isn’t recognized or if the string can’t be parsed with the given format.
Convert the results from the previous example back to strings using SUBSTR and DECODE:
Custom decoding with JavaScript UDF¶
The BINARY data type allows the storage of arbitrary data. Since JavaScript UDFs support the data type via Uint8Array
(see Introduction to JavaScript UDFs), it is possible to implement custom decoding
logic in JavaScript. This isn’t the most efficient way to work, but it is very powerful.
Create a function that decodes based on the first byte: