Home > OS >  How to convert variant to number in Snowflake
How to convert variant to number in Snowflake

Time:08-26

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")

But if I hack it to convert to varchar first and then to number, it works fine. But I don't want to use this hack.

to_number("item-price"::varchar)

Can you let me know how can I do this in a clean way?

CodePudding user response:

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. Well actually the doc's state VARIANT is supported, but it never has been. 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)

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
[ 1234 ] 1,234
  • Related