繁体   English   中英

如何在 Snowflake 中将 hash 字符串转换为 integer?

[英]How to convert a hash string to an integer in Snowflake?

我正在尝试获取十进制值的 hash 并将其转换为 integer。 但是查询导致以下错误:

Numeric value 'b902cc4550838229a710bfec4c38cbc7eb11082367a409df9135e7f007a96bda' is not recognized

SELECT (CAST(sha2(TO_VARCHAR(ABS(12.5)), 256) AS INTEGER) % 100) AS temp_value

在 Snowflake 中将 hash 字符串转换为 integer 的正确方法是什么? 我不能使用任何用户定义的函数。 并且必须具有 Snowflake 原生功能的 go。

hash 值包含字母字符,因此会引发错误

 SELECT --(CAST( sha2( TO_VARCHAR( ABS(12.5)), 256)-- AS INTEGER) % 100) AS temp_value;

您需要将 hash 编码的十六进制值转换为 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

如果您在链接中使用 function,那么您的代码将变成这样:

SELECT (CAST(js_hextoint(sha2(TO_VARCHAR(ABS(12.5)), 256)) AS INTEGER) % 100) AS temp_value

恐怕我无法测试上面的代码,所以可能是括号放错了地方......

您有一个 56 位的十六进制数。 它不适合 38 的最大数字精度。您可以使用浮点数,但这会失去精度。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM