Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

sql - Using try_cast in snowflake to deal with very long numbers

I'm using try_cast in snowflake to convert any long values in sql to NULL.

Here is my code:

When I try running the above code, I'm getting the error as below:

I'm flattening a JSON array and using try_cast to make any large values to NULL because I was getting an error Failed to cast variant value {numberLong: -8301085358432}

SELECT try_cast(item.value:price) as item_price,
       try_cast(item.value:total_price_bill) as items_total_price
FROM table, LATERAL FLATTEN(input => products) item

Error:

SQL compilation error error at line 1 at position ')'.

I don't understand where I'm doing wrong

question from:https://stackoverflow.com/questions/65923578/using-try-cast-in-snowflake-to-deal-with-very-long-numbers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you are using wrong syntax for try_cast. according to snowflake documentations the syntax is :

TRY_CAST( <source_string_expr> AS <target_data_type> )

and also note:

  1. Only works for string expressions.
  2. target_data_type must be one of the following:
    • VARCHAR (or any of its synonyms)
    • NUMBER (or any of its synonyms)
    • DOUBLE
    • BOOLEAN
    • DATE
    • TIME
    • TIMESTAMP, TIMESTAMP_LTZ, TIMESTAMP_NTZ, or TIMESTAMP_TZ

so for example you have to have something like this if item.value:price is string:

select try_cast(item.value:price as NUMBER) as item_price,
....

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...