日期:2014-05-18  浏览次数:20406 次

从数据类型 varchar 转换为 numeric 时出错
book05 是decimal(10, 2)  
sql语句报错:从数据类型 varchar 转换为 numeric 时出错。
SQL code


select
book05=case  when book05 is null or book05='' then '('+ltrim(str(count(*)))+')'  
else 
ltrim(str(book05)) + '('+ltrim(str(count(*)))+')' end 
from books
group by book05 order by book05




------解决方案--------------------
SQL code


select
book05=case  when book05 is null or book05='' --这里出错,book05是数字类型,不能这样
then '('+ltrim(str(count(*)))+')'  
else 
ltrim(str(book05)) + '('+ltrim(str(count(*)))+')' end 
from books
group by book05 order by book05

select
book05=case  when book05 is null--这么就行了
then '('+ltrim(str(count(*)))+')'  
else 
ltrim(str(book05)) + '('+ltrim(str(count(*)))+')' end 
from books
group by book05 order by book05

------解决方案--------------------
SQL code

select
book05=case  when isnull(book05,0)=0 then '('+ltrim(str(count(*)))+')'  
else 
ltrim(str(book05)) + '('+ltrim(str(count(*)))+')' end 
from books
group by book05 order by book05

------解决方案--------------------
探讨
book05 是decimal(10, 2)
sql语句报错:从数据类型 varchar 转换为 numeric 时出错。
SQL code


select
book05=case when book05 is null or book05='' then '('+ltrim(str(count(*)))+')'
else
ltrim(str(book05)) + '('+lt……

------解决方案--------------------
SQL code
select
book05=case  when book05 is null or book05='' then '('+ltrim(count(*))+')'  
else 
ltrim(book05) + '('+ltrim(count(*))+')' end 
from books
group by book05
order by book05