2

I got this working to return an integer:

CREATE FUNCTION my_function() RETURNS INTEGER AS $$
  SELECT 1 AS result;
$$ LANGUAGE SQL;

But I want it to return a string, so I tweaked it to:

CREATE FUNCTION my_function() RETURNS TEXT AS $$
  SELECT '1' AS result;
$$ LANGUAGE SQL;

But this fails with:

ERROR:  return type mismatch in function declared to return text
DETAIL:  Actual return type is unknown.

What happened? How can I return the string '1'?

2
  • '1' by default is varchar. you need to specify explicit typecasting : CREATE FUNCTION my_function() RETURNS TEXT AS $$ SELECT '1'::TEXT AS result; $$ LANGUAGE SQL; Commented Jun 5, 2017 at 20:21
  • @IlyaDyoshin - Your comment is correct generally, but '1' by default shows unknown Commented Jun 5, 2017 at 20:33

1 Answer 1

2

String literals are of type unknown. You can explicitly specify the type like this:

SELECT TEXT '1' AS result;
Sign up to request clarification or add additional context in comments.

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.