简体   繁体   English

如何在雪花中将变量转换为数字

[英]How to convert variant to number in Snowflake

I have a variant type field in snowflake, that I'm trying to convert to number.我在雪花中有一个变体类型字段,我正在尝试将其转换为数字。 If I just cast directly to number it says "Numeric value '' is not recognized" to_number("item-price")如果我只是直接转换为数字,它会说“无法识别数值''” to_number("item-price")

But if I hack it to convert to varchar first and then to number, it works fine.但是,如果我将其破解为先转换为 varchar,然后再转换为数字,它就可以正常工作。 But I don't want to use this hack.但我不想使用这个黑客。 to_number("item-price"::varchar) to_number("项目价格"::varchar)

Can you let me know how can I do this in a clean way?你能告诉我如何以干净的方式做到这一点吗?

to_number("item-price"::varchar)

is not a hack, a the TRY_/TO_NUMBER function expects TEXT/VARCHAR as input, and VARIANT is not TEXT.不是 hack, TRY_/TO_NUMBER function 期望 TEXT/VARCHAR 作为输入,而 VARIANT 不是 TEXT。 Well actually the doc's state VARIANT is supported, but it never has been.好吧,实际上文档的 state VARIANT 是受支持的,但从来没有。 It seems the doc's should be fixed.似乎应该修复文档。

Thus the requirement to cast it.因此要求铸造它。

select parse_json('[1234]') as j
    ,try_to_number(j[0]) as from_var
    ,try_to_number(j[0]::text) as from_text;

Function TRY_CAST cannot be used with arguments of types VARIANT and NUMBER(38,0) Function TRY_CAST 不能与 VARIANT 和 NUMBER(38,0) 类型的 arguments 一起使用

select parse_json('[1234]') as j
    //,try_to_number(j[0]) as from_var
    ,try_to_number(j[0]::text) as from_text;
J Ĵ FROM_TEXT FROM_TEXT
[ 1234 ] [1234] 1,234 1,234

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

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