13

I want to CREATE CAST a suitable function to convert 'character varying' to 'integer'. Can anyone suggest a function? Everything I try fails.

2
  • 5
    You should fix your SQL to not rely in implict casts. Do not rely on implict casts in your statements, never. Commented Jan 21, 2011 at 15:54
  • I agree. However I cannot I'm using a off-the-shelf piece of software. Commented Jan 21, 2011 at 15:59

4 Answers 4

29

Use this:

CREATE CAST (varchar AS integer) WITH INOUT [AS IMPLICIT];

It uses the input/output functions of the data types involved.

Sign up to request clarification or add additional context in comments.

1 Comment

More than 3k views/sp/queries to review&fix implicit casts... then you came with this f**** easy workaround. It'll get me so much more time. Thank you, job saver! (I'm wondering why postgres developers didn't put a parameter in postgresql.conf for this...)
1

The function above works if you update your SQL to do an explicit cast. However, if you add 'as implicit' to the end of your "create cast" statement, Postgres will be able to automatically figure out what you are trying to do when you compare an integer with a varchar that can be converted to an integer.

Here is my updated statement that seemed to work "implicitly":

CREATE FUNCTION toint(varchar) 
  RETURNS integer 
  STRICT IMMUTABLE LANGUAGE SQL AS 
'SELECT cast($1 as integer);';

CREATE CAST (varchar AS integer) WITH FUNCTION toint(varchar) as Implicit;

1 Comment

This function was helpful for me to cast varchar fields as a regular DB user issuing queries rather than casting the type itself.
0

I assume you want to get back the behaviour of Postgres 8.3 regarding implicit casting.

See this blog entry for examples:
http://petereisentraut.blogspot.com/2008/03/readding-implicit-casts-in-postgresql.html

Oh: and bug that vendor of the software to fix the broken SQL ;)

Edit

This should do it:

CREATE FUNCTION toint(varchar) 
  RETURNS integer 
  STRICT IMMUTABLE LANGUAGE SQL AS 
'SELECT cast($1 as integer);';

CREATE CAST (varchar AS integer) WITH FUNCTION toint(varchar);

4 Comments

Yeah. I found that but it's missing int4(varchar)
It contains a cast for integer which is the same as int4 if I'm not mistaken.
Forgive me if I'm being stupid but none of the functions return an integer. I expect to do CREATE CAST (varchar as integer)...
Forgive me! I was stupid. I thought you wanted the 8.3 behaviour back (int to varchar).
0

I got the same error. I have a COLUMN

code

declared as type character varying(20) and a local variable

l_code

declared as type int.

I solved replacing in the procedure

SELECT 
...
WHERE
code = l_code AND
..

with

code = cast( l_code as character varying)   AND

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.