SQL自定义函数-取得当前日期的季节

--作用,在SQL2000中自定义函数中不能调用getdate(),只能这样了
create view view_getmonth as select mymonth=month(getdate());
--函数体
create function fun_print_season()--函数名
returns char(4)--返回类型
as
   begin
       declare @t1 int,@t2 varchar(20) --定义局部变量
       select  @t1= mymonth from view_getmonth
       if @t1=12 OR @t1=1 OR @t1=2
           set @t2= '冬天'
       else if @t1>=3 AND @t1<=5
           set @t2=  '春天'
       else if @t1>=6 AND @t1<=8
           set @t2=  '夏天'
       else
           set @t2= '秋天'
       return @t2
   end
--调用方法
select dbo.fun_print_season() as season

你可能感兴趣的:(sql)