简体   繁体   English

将varchar值转换为int数据类型时,SQL Select Case Conversion失败。

[英]SQL Select Case Conversion failed when converting the varchar value to data type int.

Folks, I've read the fixes for this issue but I have a variation that I can't solve. 亲爱的,我已经阅读了此问题的修复程序,但是有一些我无法解决的变化。

Firstly the following syntax throws the error 首先,以下语法引发错误

 Case
    when #TFs.lTFID = 1161165 then REPLACE(CONVERT(VARCHAR(11), cast([dCreatedUTC] as datetime), 106), ' ', '-')
    when #TFs.lTFID = 1161166 then 'Administrator'
    when #TFs.lTFID = 1161167 then ''
    when #TFs.lTFID = 1161168 then AssetID
    when #TFs.lTFID = 1161169 then ''
    when #TFs.lTFID = 1161170 then ''
    when #TFs.lTFID = 1161172 then ''
    when #TFs.lTFID = 1161173 then ''
    else CAST(#TFs.lTFID as varchar(20))
  End 'Value'

If I comment out all but one of the "when" lines as below it works. 如果我注释掉下面的“何时”行之一,则全部删除。

 Case
    when #TFs.lTFID = 1161165 then REPLACE(CONVERT(VARCHAR(11), cast([dCreatedUTC] as datetime), 106), ' ', '-')
/*  when #TFs.lTFID = 1161166 then 'Administrator'
    when #TFs.lTFID = 1161167 then ''
    when #TFs.lTFID = 1161168 then AssetID
    when #TFs.lTFID = 1161169 then ''
    when #TFs.lTFID = 1161170 then ''
    when #TFs.lTFID = 1161172 then ''
    when #TFs.lTFID = 1161173 then ''*/
    else CAST(#TFs.lTFID as varchar(20))
  End 'Value'

Any thoughts about how I can format this query so that all "cases" work? 关于如何格式化此查询以使所有“案例”都有效的任何想法?

Based on the error message, the result of the CASE expressions gets converted to INT . 根据错误消息, CASE表达式的结果将转换为INT Also of the return value of your WHEN s is known to be VARCHAR , except for AssetID , so I assume this must be an INT value. 另外,除了AssetID之外,您的WHEN的返回值也称为VARCHAR ,因此我认为这必须是INT值。

The error happens because when using a CASE expression, all return values must be of the same data type. 发生错误是因为使用CASE表达式时,所有返回值必须具有相同的数据类型。 In case they have different data types, all values are converted to the type with a higher data type precedence . 如果它们具有不同的数据类型,则所有值都将转换为具有更高数据类型优先级的类型

And since INT has a higher precedence than VARCHAR , the results are converted to INT which caused the error. 并且由于INT的优先级高于VARCHAR ,因此结果将转换为INT ,从而导致错误。 To fix this, you have to convert AssetID to VARCHAR(n) also 要解决此问题,您还必须将AssetID转换为VARCHAR(n)

Case
    when #TFs.lTFID = 1161165 then REPLACE(CONVERT(VARCHAR(11), cast([dCreatedUTC] as datetime), 106), ' ', '-')
    when #TFs.lTFID = 1161166 then 'Administrator'
    when #TFs.lTFID = 1161167 then ''
    when #TFs.lTFID = 1161168 then CAST(AssetID AS VARCHAR(20))
    when #TFs.lTFID = 1161169 then ''
    when #TFs.lTFID = 1161170 then ''
    when #TFs.lTFID = 1161172 then ''
    when #TFs.lTFID = 1161173 then ''
    else CAST(#TFs.lTFID as varchar(20))
End 'Value'

Try and cast the AssetID to a varchar in the case statement. 尝试在case语句中将AssetID强制转换为varchar。

Case
    when #TFs.lTFID = 1161165 then REPLACE(CONVERT(VARCHAR(11), cast([dCreatedUTC] as datetime), 106), ' ', '-')
    when #TFs.lTFID = 1161166 then 'Administrator'
    when #TFs.lTFID = 1161167 then ''
    when #TFs.lTFID = 1161168 then CAST(AssetID AS VARCHAR(20))
    when #TFs.lTFID = 1161169 then ''
    when #TFs.lTFID = 1161170 then ''
    when #TFs.lTFID = 1161172 then ''
    when #TFs.lTFID = 1161173 then ''
    else CAST(#TFs.lTFID as varchar(20))
  End 'Value'

Try this 尝试这个

Case #TFs.lTFID
 When 1161165 then REPLACE(CONVERT(VARCHAR(11), cast([dCreatedUTC] as datetime), 106), ' ', '-')
 When 1161166 then 'Administrator'
 .
 .
 .
 Else CAST(#TFs.lTFID as varchar(20)) END

Put your CAST outside the the CASE as follows: CAST放在CASE之外,如下所示:

     CAST(
             Case
                when #TFs.lTFID = 1161165 then REPLACE(CONVERT(VARCHAR(11), cast([dCreatedUTC] as datetime), 106), ' ', '-')
                when #TFs.lTFID = 1161166 then 'Administrator'
                when #TFs.lTFID = 1161167 then ''
                when #TFs.lTFID = 1161168 then AssetID
                when #TFs.lTFID = 1161169 then ''
                when #TFs.lTFID = 1161170 then ''
                when #TFs.lTFID = 1161172 then ''
                when #TFs.lTFID = 1161173 then ''
                else #TFs.lTFID
              End 
         as varchar(20)) 'Value'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL Server / SSMS,选择> Case语句错误“将varchar值'2000.000000'转换为数据类型int时转换失败。” - SQL Server/SSMS, Select > Case statement error “Conversion failed when converting the varchar value '2000.000000' to data type int.” 将 varchar 值“Assistant”转换为数据类型 int 时转换失败。 SQL 消息错误 - Conversion failed when converting the varchar value 'Assistant' to data type int. SQL Message error 将 varchar 值转换为数据类型 int 时,SQL Select Case Conversion 失败 - SQL Select Case Conversion failed when converting the varchar value to data type int 将varchar值'NULL'转换为数据类型int时,转换失败。 为什么? - Conversion failed when converting the varchar value 'NULL' to data type int. Why? 将varchar值'->'转换为数据类型int时,获取转换失败。 我该如何解决? - Getting Conversion failed when converting the varchar value ' --> ' to data type int. How do I solve this? 为什么将varchar值“ This is full”转换为数据类型int时转换失败? - Why am I getting `Conversion failed when converting the varchar value 'This is full' to data type int.`? 错误“将 varchar 值 'ABC' 转换为数据类型 int 时转换失败。” - Error "Conversion failed when converting the varchar value 'ABC' to data type int." 将varchar值转换为数据类型int时,转换失败。 [SQLSTATE 22018](错误245) - Conversion failed when converting the varchar value to data type int. [SQLSTATE 22018] (Error 245) 在SQL中将varchar值转换为数据类型int时转换失败 - Conversion failed when converting varchar value to data type int in SQL 将varchar值'%'转换为数据类型int SQL时转换失败 - Conversion failed when converting the varchar value '%' to data type int SQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM