将varchar转换为int时出错??不解,求助!~

lixinramaxel41658133 2009-04-10 12:29:17
declare @i int , @j int
set @i=100
set @j=88
select (case when @i is null then 0 when @i=0 then 0 else convert(varchar(10),convert(decimal(10,2),@j*1.0/@i*100))+'%' end) as jsjhl

提示为:消息 245,级别 16,状态 1,第 4 行
在将 varchar 值 '88.00%' 转换成数据类型 int 时失败。

我不放到case when 里的时候又OK..什么原因?
...全文
110 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Rotel-刘志东 2009-04-10
  • 打赏
  • 举报
回复
declare @i int , @j int 
set @i=100
set @j=88
select (case when @i is null then '0' when @i=0 then '0' else convert(varchar(10),convert(decimal(10,2),@j*1.0/@i*100))+'%' end) as jsjhl

转换结果有问题呀!
pt1314917 2009-04-10
  • 打赏
  • 举报
回复

declare @i int , @j int
set @i=100
set @j=88
select (case when isnull(@i,'')=0 then '0' else convert(varchar(10),convert(decimal(10,2),@j*1.0/@i*100))+'%' end) as jsjhl
knifewei 2009-04-10
  • 打赏
  • 举报
回复
一楼正解
you_tube 2009-04-10
  • 打赏
  • 举报
回复
declare @i int , @j int 
set @i=100
set @j=88
select (case when @i is null then '0' when @i=0 then '0' else convert(varchar(10),convert(decimal(10,2),@j*1.0/@i*100))+'%' end) as jsjhl


case when 条件 then 结果1 else 结果2
结果类型要是一样的,不然会做向上转换,这里是转换出错
  • 打赏
  • 举报
回复
case when @i is null then 0 when @i=0 then 0

应该是这个原因:这块是把@i与整形数据比较,那么case的结果是整形的。
nalnait 2009-04-10
  • 打赏
  • 举报
回复

declare @i int , @j int
set @i=100
set @j=88
select (case when @i is null then '0' when @i=0 then '0' else convert(varchar(10),convert(decimal(10,2),@j*1.0/@i*100))+'%' end) as jsjhl
  • 打赏
  • 举报
回复
多谢各位!~感激了,,分少不成敬意!~

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧