0 Replies - 2307 Views - Last Post: 02 September 2012 - 04:36 PM Rate Topic: -----

#1 Momentum  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 18-October 09

[PL/pgsql] Convert character varying to an integer

Posted 02 September 2012 - 04:36 PM

Hi,

browsing the internet I found a few solutions. While trying out the solutions, I combined some the ideas and found the following code works.
But is it efficient and is it a trustful solution? Basically casting a numeric type to an integer


CREATE OR REPLACE FUNCTION "convToInt"(valuearg character varying)
  RETURNS integer AS
$BODY$DECLARE

result int;

BEGIN
EXECUTE 'SELECT to_number(quote_literal($1), quote_literal(99999999))' INTO result
USING valuearg;

RETURN result;


END$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION "convToInt"(character varying)
  OWNER TO postgres;



Is This A Good Question/Topic? 0
  • +

Page 1 of 1