日期:2014-05-17  浏览次数:20725 次

sql语句中的case when then
select case when data.windd in ( '/////', ',,,,,', '-----', '999.9', '',
'null', 'NULL', null ) then ''
when data.windd='' then ''
else convert(decimal(10, 1), data.windd) end windd
from table1

我的本意是如果这个 data.windd 这个字段值是 '/////', ',,,,,', '-----', '999.9', '',
'null', 'NULL', null 其中的一个的话 返回''空字符串,如果不是这几个特殊字符的话转换为数字,现在 一直报 从数据类型 varchar 转换为 numeric 时出错,好像是else语句不管是否满足else条件一直在判断是否能转换。
问怎么写sql能达到我的要求
SQL

------解决方案--------------------
case when  then A else B end 

A B 必须是同类型的才行。

select case when data.windd in ( '/////', ',,,,,', '-----', '999.9', '',
'null', 'NULL', null ) then ''
when data.windd='' then ''
else convert(decimal(10, 1), data.windd) end windd  --这块要不就用字符型,要不就把上面的then 后面都换了
from table1

------解决方案--------------------
decimal(10, 1) 是浮点数类型 
 data.windd in ( '/////', ',,,,,', '-----', '999.9', '',
'null', 'NULL', null ) then ''
这句会使整个语句默认为varchar类型