CONV() function in snowflake

强颜欢笑 提交于 2020-06-09 05:21:12

问题


I'm trying to find equivalent of the CONV() MySQL function in Snowflake, but I can't seem find it.

I'm trying to do the following in Snowflake:

SELECT CONV('39ix75wf3aor7',36,10)

The result should be 15468921890196183763


回答1:


I wrote a UDF to do what CONV() does. It works, but unfortunately Javascript variables don't support numeric precision for as large as your sample.

This will work for smaller inputs, but for your large Base36 input the following happens:

15468921890196183763 --should be this result

15468921890196185000 --Javascript native variables do not have that level of precision

In case you find it useful for smaller values to covert from one base to another, here it is:

create or replace function CONV(VALUE_IN string, OLD_BASE float, NEW_BASE float)
returns string
language javascript
as
$$
    // Usage note: Loses precision for very large inputs
    return parseInt(VALUE_IN, Math.floor(OLD_BASE).toString(Math.floor(NEW_BASE)));
$$;


来源:https://stackoverflow.com/questions/60347914/conv-function-in-snowflake

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!