2017-08-29 196 views
-2

我收到此错误,我找不到原因。sql server 2012:关键字'else'附近的语法不正确

case when FormFields.fieldtype like '%date%'   
    then 'not' + fieldname + ' is null and (convert(datetime,' 
     + fieldname +',103) < ' 
     + coalesce(str(FormFields.MinDate),'1/1/1900') + ')' 
     + ' or (convert(datetime,' + fieldname +',103) > ' 
     + coalesce(str(FormFields.MaxDate), '1/1/2200') + ')' 
     + 
else  
     'not '+ fieldname + ' is null and (convert(float,'+ fieldname+') <' 
     + coalesce(str(FormFields.MinValue),'-99999999') 
     + ' or convert(float,'+ fieldname+') >' 
     + coalesce(str(FormFields.MaxValue),'99999999') +')' 
     + 
end 

前面的代码没有任何错误:

'not '+ fieldname + ' is null and (convert(float,'+ fieldname+') <' 
    + coalesce(str(FormFields.MinValue),'-99999999') 
    + ' or convert(float,'+ fieldname+') >' 
    + coalesce(str(FormFields.MaxValue),'99999999') +')' 
    + 

我只是想补充另一种情况

+3

也许在'then'行结尾的超级+符号? –

+1

结尾 –

+0

还有另外一个建筑字符串代码的一部分,这就是为什么有额外的'+' – aggicd

回答

1

您的查询2个额外的+,1在THEN部分的结束,另一个在ELSE部分的末尾。如果您需要将CASE表达式与另一个字符串结合使用,请在END之后使用+。请尝试以下方法:

case when FormFields.fieldtype like '%date%'   
    then 'not' + fieldname + ' is null and (convert(datetime,' 
     + fieldname +',103) < ' 
     + coalesce(str(FormFields.MinDate),'1/1/1900') + ')' 
     + ' or (convert(datetime,' + fieldname +',103) > ' 
     + coalesce(str(FormFields.MaxDate), '1/1/2200') + ')' 

else  
     'not '+ fieldname + ' is null and (convert(float,'+ fieldname+') <' 
     + coalesce(str(FormFields.MinValue),'-99999999') 
     + ' or convert(float,'+ fieldname+') >' 
     + coalesce(str(FormFields.MaxValue),'99999999') +')' 

end 
+ 'Any string after CASE expression' 
+0

问题解决了,但现在我收到“无效的列名'MinDate'。”和“无效的列名'MaxDate'”。“ – aggicd

+1

@aggicd所以这意味着你提供了不正确的列名,正如你在正确的查询中,你只使用'MinValue'和'MaxValue',也许根本没有'MinDate'和'MaxDate'? –

+0

有,但他们是日期时间列 – aggicd

相关问题