英文:
Sha2 function return empty/null values in snowflake
问题
我正在使用sh2函数来生成哈希值,但是如果任何列的值为null/empty,它会生成null/empty。
例如
输入
select sha2(NULL, 256);
输出
NULL
是否有办法在sha2函数内部的值为空/null时生成值?
英文:
I am using sh2 function to generate the hash value but It is generating null/empty if any of the column value has null/empty.
For example
Input
select sha2(NULL, 256);
Output
NULL
is there any way to generate value even when the value inside sha2 function is empty/null.
答案1
得分: 1
SQL中的NULL表示没有值,因此您得到的结果是预期的。
您可以创建一个查询,检查NULL并根据您想要的值返回一些预定义或随机的输出,以及存在实际值时的哈希值。示例:
创建一些测试数据:
create or replace table test_table (col1 string);
insert into test_table values ('string1'), (null), ('string2');
当不为NULL时返回哈希值
select
col1,
case
when col1 is null then '您想要获取的值而不是哈希'
else sha2(col1, 256)
end as result
from test_table;
或者,您可以输出一些随机哈希而不是预定义的字符串:
select
col1,
case
when col1 is null then sha2(random())
else sha2(col1, 256)
end as result
from test_table;
英文:
NULL in SQL, means no value, so the result you are getting is expected.
You can create a query that will check for NULLs and return some predefined or random output for them, depending on what value you want, and the hash when there is the actual value. Example:
Create some test data:
create or replace table test_table (col1 string);
insert into test_table values ('string1'), (null), ('string2');
Return hash when it is not NULL
select
col1,
case
when col1 is null then 'value you want get instead of the hash'
else sha2(col1, 256)
end as result
from test_table;
Alternatively, you can output some random hash instead of a predefined string:
select
col1,
case
when col1 is null then sha2(random())
else sha2(col1, 256)
end as result
from test_table;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论