Snowflake snippets
How to create a Javascript function and call it in Snowflake
Suppose you usually manage different measure units of beer and you want to make a function to uniform all the amount of beer in your table.
For this purpose, you can make a user-defined function directly in Snowflake using Javascript language.
You can use the following query to define the function:
CREATE OR REPLACE FUNCTION PintToMilliliters(PintValue double, PintType string )
RETURNS double
LANGUAGE javascript
AS
$$
/*
* convert given Pint value in milliliters
*/
var PINT_U = PINTTYPE.toUpperCase();
switch(PINT_U) {
case 'IMPERIAL PINT':
return(PINTVALUE * 568.26125)
break;
case 'ROYAL PINT':
return(PINTVALUE * 952)
break;
case 'SCOTTISH PINT':
return(PINTVALUE * 1696)
break;
default:
return null;
break;
}
$$;
SELECT PintToMilliliters(10, 'Imperial Pint');
SELECT PintToMilliliters(10, 'Scottish Pint');
SELECT PintToMilliliters(10, 'UK Pint');