# [PL/pgsql] Convert character varying to an integer

Page 1 of 1

## 0 Replies - 3477 Views - Last Post: 02 September 2012 - 04:36 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=290755&amp;s=356109f097c176ad24716857660f53ae&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Momentum

Reputation: 1
• 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

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}