Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong with my CASE?

Tags:

sql

postgresql

CASE WHEN table1.text IS NULL THEN table2.numbers ELSE table1.text END AS newcolumn

When running this code I keep getting this error: ERROR: CASE types character varying and integer cannot be matched

You would think that this would not cause problems since I'm creating a new column within my query. I'd also like to point out that I'm using an old version of PostgreSQL if that helps.

like image 797
user519753 Avatar asked Dec 17 '22 02:12

user519753


2 Answers

CASE WHEN table1.text IS NULL THEN table2.numbers::text ELSE table1.text END AS newcolumn
like image 118
Clodoaldo Neto Avatar answered Dec 25 '22 04:12

Clodoaldo Neto


Problem is you are trying to add table1.text and table2.numbers into a single column. These two columns are two diff data types. try following

CASE WHEN table1.text IS NULL THEN CAST(table2.numbers AS VARCHAR(50)) ELSE table1.text END AS newcolumn
like image 40
Kaf Avatar answered Dec 25 '22 05:12

Kaf