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.
asked Mar 14, 2012 at 18:25
3
CASE WHEN table1.text IS NULL THEN table2.numbers::text ELSE table1.text END AS newcolumn
answered Mar 14, 2012 at 18:28
Clodoaldo NetoClodoaldo Neto
118k26 gold badges231 silver badges256 bronze badges
0
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
answered Mar 14, 2012 at 18:28
KafKaf
33k7 gold badges56 silver badges78 bronze badges
The data type represented by table2.numbers
and table1.text
has to be the same, so it looks like in this case you’ll need to CAST
the value of table2.numbers
answered Mar 14, 2012 at 18:28
Brian DriscollBrian Driscoll
19.3k3 gold badges44 silver badges63 bronze badges
Try ISNULL
CASE WHEN ISNULL(table1.text) = 1 THEN table2.numbers ELSE table1.text END AS newcolumn
answered Mar 14, 2012 at 18:28
VinnieVinnie
3,8891 gold badge25 silver badges29 bronze badges
I think the error is pretty clear — the types of the two columns that may be used by the CASE statement aren’t compatible.
Why would you think that trying to use values from two columns of different types would not cause problems? You may be creating a new column in the result set, but it still has to have a type and that has to match all the potential values.
It may be possible in a lot of cases for it to infer the type, but that’s risky and may not necessarily be the choice that you want it to make, so it’s better for it to force you to make the decision. You’ll need to modify the type of one or the other columns in the CASE statement.
answered Mar 14, 2012 at 18:29
Anthony GristAnthony Grist
38.2k8 gold badges62 silver badges76 bronze badges
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.
asked Mar 14, 2012 at 18:25
3
CASE WHEN table1.text IS NULL THEN table2.numbers::text ELSE table1.text END AS newcolumn
answered Mar 14, 2012 at 18:28
Clodoaldo NetoClodoaldo Neto
118k26 gold badges231 silver badges256 bronze badges
0
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
answered Mar 14, 2012 at 18:28
KafKaf
33k7 gold badges56 silver badges78 bronze badges
The data type represented by table2.numbers
and table1.text
has to be the same, so it looks like in this case you’ll need to CAST
the value of table2.numbers
answered Mar 14, 2012 at 18:28
Brian DriscollBrian Driscoll
19.3k3 gold badges44 silver badges63 bronze badges
Try ISNULL
CASE WHEN ISNULL(table1.text) = 1 THEN table2.numbers ELSE table1.text END AS newcolumn
answered Mar 14, 2012 at 18:28
VinnieVinnie
3,8891 gold badge25 silver badges29 bronze badges
I think the error is pretty clear — the types of the two columns that may be used by the CASE statement aren’t compatible.
Why would you think that trying to use values from two columns of different types would not cause problems? You may be creating a new column in the result set, but it still has to have a type and that has to match all the potential values.
It may be possible in a lot of cases for it to infer the type, but that’s risky and may not necessarily be the choice that you want it to make, so it’s better for it to force you to make the decision. You’ll need to modify the type of one or the other columns in the CASE statement.
answered Mar 14, 2012 at 18:29
Anthony GristAnthony Grist
38.2k8 gold badges62 silver badges76 bronze badges
I am writing a select statement in Postgres which contains case statement as follows:
,(case when all_loc.country = 'DE' then msc_si.buyer_id else msc_si.buyer_name end) as "purchasing_group_name_buyer_name" --story
,(case when all_loc.country = 'DE' then msc_si.planner_code else mscp.description end) as "mrp_controller_name" --story
I am getting the following error. I tried with IS
instead of =
, didn’t work. Without those two case
statements the query runs perfectly.
ERROR: CASE types character varying and numeric cannot be matched SQL
state: 42804
Mureinik
295k52 gold badges303 silver badges346 bronze badges
asked Jun 1, 2017 at 21:52
1
All the branches of a case
expression should return the same datatype. One way to achieve that is to explicitly cast where needed:
,(case when all_loc.country = 'DE' then msc_si.buyer_id::varchar else msc_si.buyer_name end) as "purchasing_group_name_buyer_name"
-- Here -----------------------------------------------^
,(case when all_loc.country = 'DE' then msc_si.planner_code::varchar else mscp.description end) as "mrp_controller_name"
-- And here -----------------------------------------------^
sainu
2,6863 gold badges22 silver badges41 bronze badges
answered Jun 1, 2017 at 21:57
MureinikMureinik
295k52 gold badges303 silver badges346 bronze badges
0
SELECT "table","schema",
CASE
WHEN "size" <= 1024 Then SIZE::varchar || 'MB'
WHEN "size" > 1024 AND "size" < 1000000 Then ("SIZE"/1024)::varchar || 'GB'
WHEN "size" > 1000000 THEN ("SIZE"/1024)/1024::varchar || 'TB'
END size
FROM SVV_TABLE_INFO
order by 1;
answered Nov 1, 2019 at 13:08
Перейти к контенту
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.
asked Mar 14, 2012 at 18:25
3
CASE WHEN table1.text IS NULL THEN table2.numbers::text ELSE table1.text END AS newcolumn
answered Mar 14, 2012 at 18:28
Clodoaldo NetoClodoaldo Neto
115k25 gold badges225 silver badges250 bronze badges
0
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
answered Mar 14, 2012 at 18:28
KafKaf
32.7k7 gold badges55 silver badges78 bronze badges
The data type represented by table2.numbers
and table1.text
has to be the same, so it looks like in this case you’ll need to CAST
the value of table2.numbers
answered Mar 14, 2012 at 18:28
Brian DriscollBrian Driscoll
19.1k3 gold badges46 silver badges63 bronze badges
Try ISNULL
CASE WHEN ISNULL(table1.text) = 1 THEN table2.numbers ELSE table1.text END AS newcolumn
answered Mar 14, 2012 at 18:28
VinnieVinnie
3,8591 gold badge25 silver badges28 bronze badges
I think the error is pretty clear — the types of the two columns that may be used by the CASE statement aren’t compatible.
Why would you think that trying to use values from two columns of different types would not cause problems? You may be creating a new column in the result set, but it still has to have a type and that has to match all the potential values.
It may be possible in a lot of cases for it to infer the type, but that’s risky and may not necessarily be the choice that you want it to make, so it’s better for it to force you to make the decision. You’ll need to modify the type of one or the other columns in the CASE statement.
answered Mar 14, 2012 at 18:29
Anthony GristAnthony Grist
38.1k8 gold badges65 silver badges75 bronze badges
I am writing a select statement in Postgres which contains case statement as follows:
,(case when all_loc.country = 'DE' then msc_si.buyer_id else msc_si.buyer_name end) as "purchasing_group_name_buyer_name" --story
,(case when all_loc.country = 'DE' then msc_si.planner_code else mscp.description end) as "mrp_controller_name" --story
I am getting the following error. I tried with IS
instead of =
, didn’t work. Without those two case
statements the query runs perfectly.
ERROR: CASE types character varying and numeric cannot be matched SQL
state: 42804
Mureinik
289k51 gold badges299 silver badges337 bronze badges
asked Jun 1, 2017 at 21:52
1
All the branches of a case
expression should return the same datatype. One way to achieve that is to explicitly cast where needed:
,(case when all_loc.country = 'DE' then msc_si.buyer_id::varchar else msc_si.buyer_name end) as "purchasing_group_name_buyer_name"
-- Here -----------------------------------------------^
,(case when all_loc.country = 'DE' then msc_si.planner_code::varchar else mscp.description end) as "mrp_controller_name"
-- And here -----------------------------------------------^
sainu
2,6763 gold badges23 silver badges41 bronze badges
answered Jun 1, 2017 at 21:57
MureinikMureinik
289k51 gold badges299 silver badges337 bronze badges
0
SELECT "table","schema",
CASE
WHEN "size" <= 1024 Then SIZE::varchar || 'MB'
WHEN "size" > 1024 AND "size" < 1000000 Then ("SIZE"/1024)::varchar || 'GB'
WHEN "size" > 1000000 THEN ("SIZE"/1024)/1024::varchar || 'TB'
END size
FROM SVV_TABLE_INFO
order by 1;
answered Nov 1, 2019 at 13:08
Description of the problem:
Why does the CASE type character varying and numeric cannot be matched in the question appear?
In Postgresql, the concept of string mismatch does not appear until the operation of splicing strings.
I thought it would be no problem to use the concat function, but it is caused by the wrong place
The following is a glance at a piece of code:
EXPLAIN SELECT
se.enroll_number,
s.student_name,
CASE
WHEN pay.pay_method =’Pay College’ THEN
0
ELSE
pay.sum_pay
END AS sum_pay,
concat (
‘Paying universities’,
CASE
WHEN pay.pay_method !=’Paying colleges’ THEN
»
ELSE
» || pay.sum_pay
END
) AS remark,
pay.sum_deduct
FROM
(
SELECT
student_id,
pay_method,
SUM (pay_money) AS sum_pay,
SUM (channel_deduct) AS sum_deduct
FROM
payment_detail
WHERE
college_settlement_id = 28
GROUP BY
student_id,
pay_method
) AS pay
LEFT JOIN student AS s ON pay.student_id = s.student_id
LEFT JOIN student_enroll AS se ON pay.student_id = se.student_id
The concat method is used in the above code, because the concat method that was spliced on the else in the case when before, for the above reasons, I thought that the way of use was wrong, and I was surprised.
The above sql is a wrong demonstration. Under normal circumstances, it is not recommended to write this, because it affects the performance too much. You can use union all to query the table data in one step (with ideas for the above sql). Opinions, see how many ways to achieve?
Introduction
The error message «ERROR: CASE types character varying and numeric cannot be matched» is encountered when you are using the CASE statement in a SQL query, and you are trying to compare a character data type (such as character varying
or text
) to a numeric data type (such as integer
or decimal
).
Here is an example of a query that would trigger this error:
SELECT
*
FROM users
WHERE
CASE
WHEN age < 18 THEN 'minor'
ELSE 'adult'
END = 'minor';
In this example, the age
column is a numeric data type, but the 'minor'
and 'adult'
values being compared to it are character data types.
To fix this error, you will need to ensure that the data types being compared in the CASE
statement are compatible. There are a few different ways you can do this, depending on your specific needs:
Method 1: Cast the Numeric Value to a Character Data Type
One way to fix the error is to explicitly convert the numeric value to a character data type using a type casting function such as ::text
or ::varchar
. For example:
SELECT
*
FROM users
WHERE
CASE
WHEN age::text < '18' THEN 'minor'
ELSE 'adult'
END = 'minor';
In this revised query, the age
column is explicitly converted to a text
data type using the ::text
type casting function. This allows the CASE
statement to compare the age
column to the character data type 'minor'
without generating an error.
Method 2: Use Numeric Values Instead of Character Values in the CASE
Statement
Another way to fix the error is to use numeric values instead of character values in the CASE
statement. For example:
SELECT
*
FROM users
WHERE
CASE
WHEN age < 18 THEN 1
ELSE 2
END = 1;
In this revised query, the CASE
statement compares the age
column to the numeric values 1
and 2
instead of the character values 'minor'
and 'adult'
. This allows the query to be executed without generating an error.
Method 3: Use a Different Comparison Operator
In some cases, you may be able to fix the error by using a different comparison operator in the CASE
statement. For example, instead of using the <
operator, you could use the ILIKE
operator, which performs a case-insensitive match on character data types:
SELECT
*
FROM users
WHERE
CASE
WHEN age::text ILIKE '%18%' THEN 'minor'
ELSE 'adult'
END = 'minor';
In this revised query, the age
column is explicitly converted to a text
data type using the ::text
type casting function. The ILIKE
operator is then used to perform a case-insensitive match on the age
column and the character value '18'
. This allows the CASE
statement to be executed without generating an error.
Here are a few more examples to illustrate how you can use the methods I described above to fix the «ERROR: CASE types character varying and numeric cannot be matched» error:
Example 1: Using Method 1 (Casting the Numeric Value)
Suppose we have a table called products
with the following structure:
id | price | category |
---|---|---|
1 | 10.99 | ‘clothes’ |
2 | 9.99 | ‘clothes’ |
3 | 15.99 | ‘shoes’ |
4 | 8.99 | ‘clothes’ |
We want to retrieve all the products that have a price less than 10
and belong to the 'clothes'
category. Here’s how we can do that using the CASE
statement:
SELECT
*
FROM products
WHERE
category = 'clothes' AND
CASE
WHEN price::text < '10' THEN 'cheap'
ELSE 'expensive'
END = 'cheap';
In this query, the price
column is explicitly converted to a text
data type using the ::text
type casting function. This allows the CASE
statement to compare the price
column to the character value '10'
without generating an error.
The query would return the following rows:
id | price | category |
---|---|---|
1 | 10.99 | ‘clothes’ |
2 | 9.99 | ‘clothes’ |
4 | 8.99 | ‘clothes’ |
Example 2: Using Method 2 (Using Numeric Values)
Suppose we have a table called customers
with the following structure:
id | name | loyalty_points |
---|---|---|
1 | ‘John’ | 100 |
2 | ‘Kate’ | 50 |
3 | ‘Bob’ | 150 |
4 | ‘Alice’ | 25 |
We want to retrieve all the customers who have more than 100
loyalty points. Here’s how we can do that using the CASE
statement:
SELECT
*
FROM customers
WHERE
CASE
WHEN loyalty_points > 100 THEN 1
ELSE 0
END = 1;
In this query, the CASE
statement compares the loyalty_points
column to the numeric values 1
and 0
instead of character values. This allows the query to be executed without generating an error.
The query would return the following rows:
id | name | loyalty_points |
---|---|---|
1 | ‘John’ | 100 |
3 | ‘Bob’ | 150 |
Example 3: Using Method 3 (Using a Different Comparison Operator)
Suppose we have a table called employees
with the following structure:
id | name | position |
---|---|---|
1 | ‘Jack’ | ‘Manager’ |
2 | ‘Jill’ | ‘Developer’ |
3 | ‘John’ | ‘Salesperson’ |
4 |
Conclusion
To conclude, the «ERROR: CASE types character varying and numeric cannot be matched» error is encountered when you are using the CASE
statement in a SQL query and you are trying to compare a character data type to a numeric data type. To fix this error, you can use one of the following methods:
- Cast the numeric value to a character data type using a type casting function such as
::text
or::varchar
. - Use numeric values instead of character values in the
CASE
statement. - Use a different comparison operator that is compatible with both the numeric and character data types.
I hope this helps! Let me know if you have any questions or need further clarification on any of the concepts I discussed.