Skip to content

Instantly share code, notes, and snippets.

@ozzyaaron
Created June 13, 2011 09:02
Show Gist options
  • Save ozzyaaron/1022494 to your computer and use it in GitHub Desktop.
Save ozzyaaron/1022494 to your computer and use it in GitHub Desktop.
def self.up
function_string = 'CREATE OR REPLACE FUNCTION searchable_number(v_input text)
RETURNS numeric AS $$
DECLARE v_numeric_value numeric DEFAULT NULL;
BEGIN
BEGIN
v_numeric_value := v_input::NUMERIC;
EXCEPTION WHEN OTHERS THEN
RAISE NOTICE \'Invalid numeric value: "%". Returning NULL.\', v_input;
RETURN NULL;
END;
RETURN v_numeric_value;
END;
$$ LANGUAGE plpgsql;'
execute function_string
function_string = 'CREATE OR REPLACE FUNCTION searchable_datetime(v_input text)
RETURNS timestamp AS $$
DECLARE v_timestamp_value timestamp DEFAULT NULL;
BEGIN
BEGIN
v_timestamp_value := v_input::TIMESTAMP;
EXCEPTION WHEN OTHERS THEN
RAISE NOTICE \'Invalid datetime value: "%". Returning NULL.\', v_input;
RETURN NULL;
END;
RETURN v_timestamp_value;
END;
$$ LANGUAGE plpgsql;'
execute function_string
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment