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