SQL Server 返回两个时间的小时部分之差
create function gethours(@stime datetime,@etime datetime)
returns int
as
begin
declare @hstime nvarchar(30)
declare @hetime nvarchar(30)
select @hstime=Ltrim(datepart(hh,@stime))
select @hetime=Ltrim(datepart(hh,@etime))
if @hstime<8 and @hetime<17 and @hetime>8
return @hetime-8
else if @hstime<8 and @hetime>17
return 8
else if @hstime<8 and @hetime<8
return 0
else if @hstime>17 and @hetime>17
return 0
else if @hstime>17 and @hetime<17 and @hetime>8
return @hetime-8
else if @hstime>17 and @hetime<8
return 0
else
return @hetime-@hstime
end
提示信息:
消息 455,级别 16,状态 2,过程 gethours,第 22 行
The last statement included within a function must be a return statement.
不知道怎么解决啊
------解决方案--------------------如果你是由什么特殊的需求,我把你的函数改了一下,应该不会报错了:
create function gethours(@stime datetime,@etime datetime)
returns int
as
begin
declare @hstime nvarchar(30)
declare @hetime nvarchar(30)
declare @result int
select @hstime=Ltrim(datepart(hh,@stime))
select @hetime=Ltrim(datepart(hh,@etime))
if @hstime<8 and @hetime<17 and @hetime>8
set @result=@hetime-8
else if @hstime<8 and @hetime>17
set @result=8
else if @hstime<8 and @hetime<8
set @result=0
else if @hstime>17 and @hetime>17
set @result=0
else if @hstime>17 and @hetime<17 and @hetime>8
set @result=@hetime-8
else if @hstime>17 and @hetime<8
set @result=0
else
set @result=(cast(@hetime as int)- cast(@hs