简体   繁体   English

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

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

With the help of case statement, I would like to get the result as "Non Revenue" in Geodept column where the value is 0在案例陈述的帮助下,我想在值为 0 的 Geodept 列中获得“非收入”的结果

Simple case statement - (gives conversion error)简单的 case 语句 - (给出转换错误)

case when GeoDeptId = 0 then 'Non Revenue' else GeoDeptId end

So tried as below but getting value as 0 again所以尝试如下但再次获得价值为0

convert(int, case when IsNumeric(convert(int, rd.GeoDeptId ))= 0 then convert (varchar(14),'Non Revenue') else rd.GeoDeptId
end ),

So tried as below but getting value as 0 again所以尝试如下但再次获得价值为0

convert(int, case when IsNumeric(convert(int, rd.GeoDeptId ))= 0 then convert (varchar(14),'Non Revenue') else rd.GeoDeptId
end )

Cast the GeoDeptId to text:GeoDeptId为文本:

CASE WHEN GeoDeptId = 0 THEN 'Non Revenue' ELSE CAST(GeoDeptId AS varchar(max)) END

The source of your error is that all branches of a CASE expression must have the same type.错误的根源是CASE表达式的所有分支必须具有相同的类型。 Since it is not possible for 'Non Revenue' to be cast to an integer, therefore we can convert the GeoDeptId to text.由于无法将'Non Revenue'转换为整数,因此我们可以将GeoDeptId转换为文本。

暂无
暂无

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

相关问题 将varchar值转换为int数据类型时,SQL Select Case Conversion失败。 - SQL Select Case Conversion failed when converting the varchar value to data type int. 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值'%'转换为数据类型int SQL时转换失败 - Conversion failed when converting the varchar value '%' to data type int SQL 在SQL中将varchar值转换为数据类型int时转换失败 - Conversion failed when converting varchar value to data type int in SQL 将varchar值int转换为数据类型int时转换失败 - Conversion failed when converting the varchar value int to data type int CASE语句:将varchar值“不需要ed”转换为数据类型int时,转换失败 - CASE statement: Conversion failed when converting the varchar value 'no ed is required' to data type int 将varchar值'test'转换为数据类型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 将varchar值'%'转换为数据类型int时转换失败 - Conversion failed when converting the varchar value '%' to data type int Error 将varchar值'Id'转换为数据类型int时转换失败 - Conversion failed when converting the varchar value 'Id' to data type int 将varchar值'1 * 2 * 3'转换为数据类型int时转换失败 - Conversion failed when converting the varchar value '1*2*3' to data type int
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM