在我的数据中,我用逗号分隔了字符串。如果这些是数组,会容易得多,因此我可以轻松地将它们与另一个数组进行匹配。但是,我无法从字符串创建数组。
示例代码:
create table tmp_array_string as
select '"abc", "def"' as tmp_string
, array("abc", "def") as tmp_array
select a.*
, array(tmp_string) as not_a_proper_array
, size(tmp_array) as array_size
, size(array(tmp_string) ) not_the_proper_array_size
from tmp_array_string a
为什么array(tmp_string不能生成包含2个元素的数组,有没有办法使它起作用?
非常感谢!
select a.*
, array(tmp_string) as not_a_proper_array
, split(regexp_replace(tmp_string,'"',''),',\\s*') as correct_array
, size(tmp_array) as array_size
, size(split(regexp_replace(tmp_string,'"',''),',\\s*')) as correct_size
, size(array(tmp_string) ) not_the_proper_array_size
from tmp_array_string ater code here
正确的大小会导致2:-)
答案 0 :(得分:0)
使用split(str, ',')
获取一个数组。请参阅dosc:here
select split('abc, def',',\\s*'); --Delimiter is comma+zero or more spaces
结果:
OK
["abc","def"]
Time taken: 1.676 seconds, Fetched: 1 row(s)
如果定界字符串中包含多余的双引号,请使用regex_replace
将其删除:
select split(regexp_replace('"abc", "def"','"',''),',\\s*');
结果:
OK
["abc","def"]
Time taken: 3.735 seconds, Fetched: 1 row(s)