日期:2014-05-19  浏览次数:20390 次

有没有条件表达式的函数
我想让一个字段等于a时输出1,等于b时输出2,用case   when当然可以做到,但很麻烦,我的sql语句出现了多次这种类似的比较,有没有类似于isnull这种函数能方便地进行比较啊。谢谢

------解决方案--------------------
好像没有
------解决方案--------------------
用case 判断或nullif( 'a ',col1)is null then 1
------解决方案--------------------
我给你示范一下
CREATE FUNCTION dbo.myout (@myin varchar(10))
RETURNS int AS
BEGIN
return
case when @myin= 'a ' then 1
when @myin= 'b ' then 2
when @myin= 'b ' then 3
else 3
end
END
----------------------
select dbo.myout( 'a ')
select dbo.myout( 'b ')