Skip to content

Instantly share code, notes, and snippets.

@robinsmidsrod
Created October 22, 2018 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robinsmidsrod/5e847153633253225b74132310bd40b1 to your computer and use it in GitHub Desktop.
Save robinsmidsrod/5e847153633253225b74132310bd40b1 to your computer and use it in GitHub Desktop.
CREATE FUNCTION public.datatype_or_null(data anyelement, datatype text)
RETURNS anyelement
LANGUAGE plpgsql IMMUTABLE
AS $$
BEGIN
RETURN CAST(data AS datatype);
EXCEPTION
WHEN OTHERS THEN
RETURN CAST(NULL AS datatype);
END;
$$;
select datatype_or_null('123'::text, 'integer');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment