在AWS Redshift中,json_extract_path_text函数返回空字符串而不是NULL。

huangapple go评论70阅读模式
英文:

In AWS redshift, the json_extract_path_text function return empty string instead NU

问题

In AWS Redshift, when using the json_extract_path_text function in a standalone select, it returns NULL as expected. However, when used in a select from a table, it returns an empty string (""). To make the function return NULL from a select table query, you can modify your SQL query like this:

select CASE WHEN json_extract_path_text(payload, 'AA') = '' THEN NULL ELSE json_extract_path_text(payload, 'AA') END from #test;

This query will check if the result is an empty string and return NULL if it is.

英文:

In AWS Redshift, I am using json_extract_path_text function to parse simple Json.
I deliberately try to find non exists element in the Json so the function should return NULL.
If I use the function in 'standalone' select, the function return NULL as expected but if the function is used in select from table then a string empty ("") returned.
Here a simple example:

select json_extract_path_text('{"A":"B"}', 'AA'); --> Return NULL as expected.

create table #test (
    payload varchar(max)
);
insert into #test (payload) values ('{"A":"B"}');

select json_extract_path_text(payload,'AA') from #test; --> Return empty string

在AWS Redshift中,json_extract_path_text函数返回空字符串而不是NULL。

在AWS Redshift中,json_extract_path_text函数返回空字符串而不是NULL。

How can I make the function return null from select table query?
Thanks.

I try everything...

答案1

得分: 1

我向AWS发布了这个问题,他们批准了这是Redshift中的一个错误:
AWS支持:
"我已经与我们的内部团队联系过,他们提到不一致实际上是Redshift一侧的一个错误,仍在由服务团队处理,并将在即将发布的补丁中解决"。

英文:

I posted to AWS the issue and they approved that this is a bug in Redshift:
AWS support:
"I have been in contact with our internal team and they mentioned that the inconsistency is actually a bug from Redshift side that is still being worked on by the Service team and will be addressed in the upcoming patches"

答案2

得分: 0

你需要使用 NULLIF 函数

select nullif(json_extract_path_text(payload,'AA'),'') from #test
英文:

You need to use the NULLIF function

select nullif(json_extract_path_text(payload,'AA'),'') from #test

huangapple
  • 本文由 发表于 2023年5月11日 19:49:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76227333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定