简体   繁体   English

CASE语句:将varchar值“不需要ed”转换为数据类型int时,转换失败

[英]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.

相关问题 将varchar值&#39;test&#39;转换为数据类型int错误时转换失败,但是CONVERT函数位于未命中的CASE语句中 - Conversion failed when converting the varchar value 'test' to data type int error but the CONVERT function is in a CASE statement that is not hit SQL Server / SSMS,选择&gt; Case语句错误“将varchar值&#39;2000.000000&#39;转换为数据类型int时转换失败。” - SQL Server/SSMS, Select > Case statement error “Conversion failed when converting the varchar value '2000.000000' to data type int.” 将varchar值int转换为数据类型int时转换失败 - Conversion failed when converting the varchar value int to data type int 将 varchar 值转换为数据类型 int 时,SQL Select Case Conversion 失败 - SQL Select Case Conversion failed when converting the varchar value to data type int 将varchar值转换为int数据类型时,SQL Select Case Conversion失败。 - SQL Select Case Conversion failed when converting the varchar value to data type int. 将varchar值&#39;durationms&#39;转换为数据类型int时转换失败 - Conversion failed when converting the varchar value 'durationms' to data type int 将varchar值&#39;..&#39;转换为数据类型int时转换失败 - Conversion failed when converting the varchar value '..' to data type int 将varchar值&#39;@a&#39;转换为数据类型int时转换失败 - Conversion failed when converting the varchar value '@a' to data type int 将 varchar 值“1 BOX”转换为数据类型 int 时转换失败 - Conversion failed when converting the varchar value '1 BOX ' to data type int 将 varchar 值“Steven”转换为数据类型 int 时转换失败 - Conversion failed when converting the varchar value 'Steven' to data type int
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM