1

I'm trying to use CASE to return a text string when a cell is a certain integer, otherwise return the original integer value.

SELECT
  CASE WHEN table.column_name = 10 THEN 'yes'
       ELSE table.column_name
  END
FROM table

I'm getting an error that 'yes' is an invalid input syntax, since it's a string and column_name is stored as integers.

2
  • 2
    You cannot do that since the result must be of a particular type. In your case it's either a string or a number. Pick one. Commented Jun 26, 2016 at 22:49
  • The question part of your question is missing. What do you want to know? Commented Jun 26, 2016 at 23:01

1 Answer 1

3

Your CASE returns either a number or a string, but the column has to be of a precise type. In the example below, I cast the input column to a string.

SELECT
  CASE WHEN table.column_name = 10 THEN 'yes'
       ELSE to_char(table.column_name, '999')
  END AS value
FROM table

The resulting column will be a String, either 'yes' or '12', '13', ...

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

1 Comment

Thank you! I was thinking the input (10) and the result ('yes') had to be the same type, and wasn't thinking at all about the ELSE clause.

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.