Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Snowflake condition type on semi-structured data

In Snowflake I have a column that holds a semi-structured data that should hold numbers but as of semi-structured data nature, that is not strict. I want to check the type of data and convert to float if possible. Something like that:

SELECT IFF(TYPEOF(column_name::data_name) is NUMBER, column_name::data_name::FLOAT, null)
FROM some_table

How can I make this query work?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

TRY_TO_DOUBLE is the tool you are looking for if you want it to be floating point. Otherwise if only Integer TRY_TO_NUMBER

SELECT try_to_double(column_name::data_name::TEXT)
FROM some_table

Here the DB doesn’t like output of type VARAINT, which is what selecting a value from a VARAINT gives you. The way around this is turn it into a TEXT first:

SELECT 
    parse_json('{"data_name":3.14, "it_is_a_number":1234}') as column_name
    ,try_to_double(column_name:data_name::text) as dub
    ,try_to_number(column_name:it_is_a_number::text) as int
    ;
COLUMN_NAME DUB INT
{ "data_name": 3.14, "it_is_a_number": 1234 } 3.14 1,234
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading