[英]Data type casting inside a CASE statement
I am using Informix and have a CASE statement in a query as below: 我正在使用Informix,并且在查询中具有CASE语句,如下所示:
CASE
when l.src_country = 'USA' then tl.cat--prodUSA.description
when l.src_country = 'CAN' AND tl.fuel_type > 0 then tl.fuel_type
when l.src_country = 'CAN' and tl.fuel_type = 0 and tl.cat in ("DEF","DEFD") then 4194304
when l.src_country = 'CAN' and tl.fuel_type = 0 and tl.cat not in ("DEF","DEFD") then tl.fuel_type
else 0
END as product_code
In the above statement, "tl.cat"
is of CHAR type & "tl.fuel_type"
is of the type INT. 在上面的语句中,
"tl.cat"
为CHAR类型, "tl.fuel_type"
为INT类型。
I need to do something like this: 我需要做这样的事情:
CASE
when l.src_country = 'USA' then tl.cat--prodUSA.description
when l.src_country = 'CAN' AND tl.fuel_type > 0 then tl.fuel_type :: CHARACTER
when l.src_country = 'CAN' and tl.fuel_type = 0 and tl.cat in ("DEF","DEFD") then 4194304
when l.src_country = 'CAN' and tl.fuel_type = 0 and tl.cat not in ("DEF","DEFD") then tl.fuel_type :: CHARACTER
else 0
END as product_code
But when I try it, I'm getting the below error: 但是,当我尝试它时,出现以下错误:
Corresponding data types must be compatible in CASE expression or DECODE function. [SQL State=IX000, DB Errorcode=-800]
When I try this: 当我尝试这个:
CASE
when l.src_country = 'USA' then tl.cat--prodUSA.description
when l.src_country = 'CAN' AND tl.fuel_type > 0 then tl.fuel_type :: CHAR
when l.src_country = 'CAN' and tl.fuel_type = 0 and tl.cat in ("DEF","DEFD") then '4194304'
when l.src_country = 'CAN' and tl.fuel_type = 0 and tl.cat not in ("DEF","DEFD") then tl.fuel_type :: CHAR
else '0'
END as product_code
I'm getting the below error: 我收到以下错误:
Converted value does not fit into the allotted space
I need to cast the tl.fuel_type
as a Character. 我需要将
tl.fuel_type
为Character。 What am I doing wrong here? 我在这里做错了什么?
Suppose we have a one table setup like this: 假设我们有一个这样的单表设置:
DROP TABLE IF EXISTS fuel_info;
CREATE TEMP TABLE fuel_info
(
src_country CHAR(3) NOT NULL,
fuel_type INTEGER NOT NULL,
cat CHAR(9) NOT NULL
);
INSERT INTO fuel_info VALUES('USA', 12, 'CAT1');
INSERT INTO fuel_info VALUES('CAN', 10, 'ABC2');
INSERT INTO fuel_info VALUES('CAN', 0, 'DEFD');
INSERT INTO fuel_info VALUES('CAN', 0, 'WXYZ');
INSERT INTO fuel_info VALUES('EUR', 1, 'P3X9');
We can adapt the expressions in the question to reproduce the problem. 我们可以调整问题中的表达式以重现问题。 For example:
例如:
SELECT
CASE
WHEN tl.src_country = 'USA' THEN tl.cat--prodUSA.description
WHEN tl.src_country = 'CAN' AND tl.fuel_type > 0 THEN tl.fuel_type::CHAR
WHEN tl.src_country = 'CAN' AND tl.fuel_type = 0 AND tl.cat IN ("DEF","DEFD") THEN '4194304'
WHEN tl.src_country = 'CAN' AND tl.fuel_type = 0 AND tl.cat NOT IN ("DEF","DEFD") THEN tl.fuel_type::CHAR
ELSE '0'
END AS product_code
FROM fuel_info AS tl;
This generates the SQL -1207: Converted value does not fit into the allotted space
error. 这将生成
SQL -1207: Converted value does not fit into the allotted space
错误。
You can fix it by specify big enough lengths for the CHAR
type casts: 您可以通过为
CHAR
类型转换指定足够长的长度来修复它:
SELECT
CASE
WHEN tl.src_country = 'USA' THEN tl.cat--prodUSA.description
WHEN tl.src_country = 'CAN' AND tl.fuel_type > 0 THEN tl.fuel_type::CHAR(7)
WHEN tl.src_country = 'CAN' AND tl.fuel_type = 0 AND tl.cat IN ("DEF","DEFD") THEN '4194304'
WHEN tl.src_country = 'CAN' AND tl.fuel_type = 0 AND tl.cat NOT IN ("DEF","DEFD") THEN tl.fuel_type::CHAR(7)
ELSE '0'
END AS product_code
FROM fuel_info AS tl;
This works, producing: 这有效,产生:
CAT1
10
4194304
0
0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.