[英]CASE statement: Conversion failed when converting the varchar value 'no ed is required' to data type int
I have a CASE
statement in my code, I want to have a string on the ELSE '' END
but get this error: 我的代码中有一个
CASE
语句,我想在ELSE '' END
上有一个字符串,但出现此错误:
Conversion failed when converting the varchar value 'no ed is required' to data type int.
将varchar值“不需要”转换为数据类型int时,转换失败。
This is my code: 这是我的代码:
SELECT
[Ticket ID],
[Request Type],
MAX(maxBuild),
(CASE
WHEN [edBuild Date Documented] IS NOT NULL
AND [edBuild Date Documented] > MAX(maxBuild)
THEN (DATEDIFF(DAY, [edBuild Date Documented], MAX(maxVal)))
ELSE ''
END) AS 'Days took from edBuild to validation/if 0 no edbuild involved'
FROM
(SELECT DISTINCT
CreatedDateTime AS [ticket submitted],
IncidentID AS [Ticket ID],
[titleT],
(CASE
WHEN OwnedbyTeamT = 'xxx'
AND ClosedDateTimeT <> '1899-12-30 00:00:00.000'
AND TitleT LIKE '%ed%Build'
AND (TitleT NOT LIKE '%Dup%')
THEN (ClosedDateTimeT)
ELSE 'no ed is required'
END) AS 'edBuild Date Documented'
FROM
TaskView
WHERE
IncidentID = '2272516 ' OR IncidentID = '2221' OR IncidentID = '21211') AS tt
GROUP BY
[Ticket ID], [Request Type],
[MedBuild Date Documented],
MAX(maxBuild)
is there any way to resolve this error? 有什么办法可以解决此错误?
Thanks 谢谢
You can't mix the datatype .. if the result is varchar then cast the int to varchar too 您不能混合数据类型..如果结果是varchar,则也将int强制转换为varchar
THEN (CAST ClosedDateTimeT AS VARCHAR) ELSE 'no ed is required' END) AS 'edBuild Date Documented'
for 对于
THEN cast( (DATEDIFF(DAY, [edBuild Date Documented], MAX(maxVal))) as VARCHAR) ELSE '' ....
I suggest you to use NULL
instead of ''
. 我建议您使用
NULL
而不是''
。 ''
cannot be converted to int
implicitly. ''
不能隐式转换为int
。
also if your result type is needed to be int
you can use 另外,如果需要将结果类型设置为
int
,则可以使用
SELECT CAST('' AS int);
result is 0
结果是
0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.