CREATE DEFINER=`root`@`192.168.10.%` FUNCTION `get_firstday`(a datetime) RETURNS int(11)
begin
declare x datetime;
declare y int;
declare z int;
select theday,workhour into x,y from `cfg_au_dayworkhour` where left(theday,10)=left(a,10);
if y=8 then
set z=(
case when a >addtime(x,'18:00:00') then 0
     when a >addtime(x,'13:00:00') then left(timediff(addtime(x,'18:00:00'),a),2)*60+mid(timediff(addtime(x,'18:00:00'),a),4,2)
     when a>addtime(x,'12:00:00') then 5*60
     when a>addtime(x,'09:00:00') then 5*60+(left(timediff(addtime(x,'12:00:00'),a),2)*60)+(mid(timediff(addtime(x,'12:00:00'),a),4,2))
     else 8*60
     end);
else
set z=0;
end if;     
return z;
end;
看这个 吧.这个还清楚一些...

解决方案 »

  1.   

    create function get_time(@a datetime,@b datetime)
    returns int
    as
    begin
    declare @z int
    if @a>@b return 0
    if datediff(d,@a,@b)=0
    set @z=dbo.get_sameday(@a,@b)
    else
    select @z=dbo.get_firstday(@a)+dbo.get_endday(@b)+isnull(sum(workhour)*60,0) 
    from cfg_au_dayworkhour 
    where theday>convert(char(10),@a,120) and theday <convert(char(10),@b,120)
    return @z
    end
    go
      

  2.   

    mysql的语法跟sqlserver还是挺接近的
      

  3.   

    create function get_firstday(@a datetime)
    returns int
    as
    begin
    declare @x datetime,@y int,@z int
    select top 1 @x=theday,@y=workhour from cfg_au_dayworkhour where datediff(d,theday,@a)=0 if @y=8
    set @z=case when @a>dateadd(hh,18,@x) then 0
    when @a>dateadd(hh,13,@x) then datediff(hh,dateadd(hh,18,@x),@a)*60+datediff(n,dateadd(hh,18,@x),@a)
    when @a>dateadd(hh,12,@x) then 5*60
    when @a>dateadd(hh,9,@x) then 5*60+datediff(hh,dateadd(hh,12,@x),@a)*60+datediff(n,dateadd(hh,12,@x),@a)
    else 8*60
    end
    else 
    set @z=0 return @z
    end
    go