Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.