Use @DBNAME

IF  EXISTS (SELECT * FROM sysobjects WHERE name='intSt_3') 
BEGIN
   DROP PROCEDURE intSt_3
END
GO
CREATE FUNCTION [dbo].[intSt_3] (@ioBD  as char(5) , @ioBT  as char(5) , @ioED  as char(5) , @ioET  as char(5),
                       @ioBDN as char(5) , @ioBTN as char(5) , @ioEDN as char(5) , @ioETN as char(5),
                       @ioBDP as char(5) , @ioBTP as char(5) , @ioEDP as char(5) , @ioETP as char(5),
                       @ciED as char(5) , @ciET as char(5), @ciBED as char(5) , @ciBET as char(5),@ciDBD as char(5),@ciDBT as char(5),
                       @DD as char(5) , @Status varchar(20) , @Perno as char(10),
                       @Holiday as char(1) ,  @E_N_MinBefore as int , @E_N_MinAfter as int , @E_H_Min as int )  

returns varchar(50)
AS  
BEGIN 
Declare  @sumSt as BigInt, @sumDur as Int, @sumMor as Int, @sumMam as Int , @DistinctMor as smallint , @DistinctMam as smallint
set @Status = ','+@Status+','
IF(@Status = ',12,13,16,17,20,21,')
BEGIN
	select  @sumSt = isnull(sum(St),0) , @sumDur = isnull(sum(Duration),0) , @sumMor = MORKHASITYPE  , @sumMam = isnull(sum(MAMURIATTYPE),0)
	from
		(
		select   
		  St = case
		  when STATUS = 8  then  110000000000
		  when STATUS = 9  then  101000000000
		  when STATUS = 10 then 100100000000
		  when STATUS = 11 then 100010000000
		  when STATUS = 12 then 100001000000
		  when STATUS = 13 then 100000100000
		  when STATUS = 16 then 100000010000
		  when STATUS = 17 then 100000001000
		  when STATUS = 20 then 100000000100
		  when STATUS = 21 then 100000000010
		  when STATUS = 14 then 100000000001
		  end ,
		  Duration , MORKHASITYPE , MAMURIATTYPE
		from TimeTab tt
		where perno = @perno
		and (BeginDATE >= @DD) And (CalcDate >= @DD) And ((BeginDate + BeginTime <= @ciED + @ciET) Or (BeginDATE = @DD))
		and (  CHARINDEX(','+cast(Status as varchar(3))+',', @Status, 1) >0
				  or  ( CHARINDEX(','+ '14' +',', @Status, 1) >0  and Status = 21 and  WORKINGINTERVAL  in (0,1,5,6) )
				  or  ( CHARINDEX(','+ '14' +',', @Status, 1) >0  and Status = 13  and  WORKINGINTERVAL  in (0,1,5,6) )
			   )
		and not  (   CHARINDEX(','+ '13' +',', @Status, 1) <> 0 and Status = 13 and  WORKINGINTERVAL   in (0,1,5,6) )  

		and (  CHARINDEX(','+ '14' +',', @Status, 1) = 0  or  tt.BeginDate+tt.BeginTime > @ciDBD+@ciDBT  or tt.Duration >=  @E_N_MinBefore or @E_N_MinBefore = 0 )
		and (  CHARINDEX(','+ '14' +',', @Status, 1) = 0  or  tt.BeginDate+tt.BeginTime < @ciDBD+@ciDBT  or tt.Duration >=  @E_N_MinAfter or @E_N_MinAfter = 0 )
		and (  CHARINDEX(','+ '14' +',', @Status, 1) = 0  or  tt.BeginDate+tt.BeginTime < @ciED+@ciET  or  @Holiday <> '0' or tt.Duration>= @E_H_Min or @E_H_Min=0  )

		and
		( 
		( (BeginDate+BeginTime > @ioEDP+@ioETP and EndDate+EndTime < @ioBDN+@ioBTN) or (BeginDate+BeginTime = @ioED+@ioET  ) or len(@ioBT+@ioET)<10 )
		and (  @ioEDP <> @DD  or (  len(@ciBED+@ciBET)=10  and  BeginDate+BeginTime >= @ciBED+@ciBET)or (BeginDate+BeginTime = @ioED+@ioET or EndDate+EndTime = @ioED+@ioET or BeginDate+BeginTime = @ioBD+@ioBT or EndDate+EndTime = @ioBD+@ioBT Or (BeginDate+BeginTime > @ioBD+@ioBT And EndDate+EndTime < @ioED+@ioET)) )
		and  ( len(@ciBED+@ciBET)<10 or  EndDate+EndTime <= @ciBED+@ciBET or  BeginDate+BeginTime >= @ciBED+@ciBET ) 
		)
	) t1
	group by MORKHASITYPE
END
ELSE
BEGIN
	select  @sumSt = isnull(sum(St),0) , @sumDur = isnull(sum(Duration),0) , @sumMor = isnull(sum(MORKHASITYPE),0)  , @sumMam = isnull(sum(MAMURIATTYPE),0)
	from
		(
		select   
		  St = case
		  when STATUS = 8  then  110000000000
		  when STATUS = 9  then  101000000000
		  when STATUS = 10 then 100100000000
		  when STATUS = 11 then 100010000000
		  when STATUS = 12 then 100001000000
		  when STATUS = 13 then 100000100000
		  when STATUS = 16 then 100000010000
		  when STATUS = 17 then 100000001000
		  when STATUS = 20 then 100000000100
		  when STATUS = 21 then 100000000010
		  when STATUS = 14 then 100000000001
		  end ,
		  Duration , MORKHASITYPE , MAMURIATTYPE
		from TimeTab tt
		where perno = @perno
		and (BeginDATE >= @DD) And (CalcDate >= @DD) And ((BeginDate + BeginTime <= @ciED + @ciET) Or (BeginDATE = @DD))
		and (  CHARINDEX(','+cast(Status as varchar(3))+',', @Status, 1) >0
				  or  ( CHARINDEX(','+ '14' +',', @Status, 1) >0  and Status = 21 and  WORKINGINTERVAL  in (0,1,5,6) )
				  or  ( CHARINDEX(','+ '14' +',', @Status, 1) >0  and Status = 13  and  WORKINGINTERVAL  in (0,1,5,6) )
			   )
		and not  (   CHARINDEX(','+ '13' +',', @Status, 1) <> 0 and Status = 13 and  WORKINGINTERVAL   in (0,1,5,6) )  

		and (  CHARINDEX(','+ '14' +',', @Status, 1) = 0  or  tt.BeginDate+tt.BeginTime > @ciDBD+@ciDBT  or tt.Duration >=  @E_N_MinBefore or @E_N_MinBefore = 0 )
		and (  CHARINDEX(','+ '14' +',', @Status, 1) = 0  or  tt.BeginDate+tt.BeginTime < @ciDBD+@ciDBT  or tt.Duration >=  @E_N_MinAfter or @E_N_MinAfter = 0 )
		and (  CHARINDEX(','+ '14' +',', @Status, 1) = 0  or  tt.BeginDate+tt.BeginTime < @ciED+@ciET  or  @Holiday <> '0' or tt.Duration>= @E_H_Min or @E_H_Min=0  )

		and
		( 
		( (BeginDate+BeginTime > @ioEDP+@ioETP and EndDate+EndTime < @ioBDN+@ioBTN) or (BeginDate+BeginTime = @ioED+@ioET  ) or len(@ioBT+@ioET)<10 )
		and (  @ioEDP <> @DD  or (  len(@ciBED+@ciBET)=10  and  BeginDate+BeginTime >= @ciBED+@ciBET)or (BeginDate+BeginTime = @ioED+@ioET or EndDate+EndTime = @ioED+@ioET or BeginDate+BeginTime = @ioBD+@ioBT or EndDate+EndTime = @ioBD+@ioBT Or (BeginDate+BeginTime > @ioBD+@ioBT And EndDate+EndTime < @ioED+@ioET)) )
		and  ( len(@ciBED+@ciBET)<10 or  EndDate+EndTime <= @ciBED+@ciBET or  BeginDate+BeginTime >= @ciBED+@ciBET ) 
		)
	) t1
END

set @DistinctMor = cast(substring(cast(@sumSt as varchar(50)),6,1) as smallint)
if @DistinctMor = 0  --hn 8703b1
  set @DistinctMor = cast(substring(cast(@sumSt as varchar(50)),10,1) as smallint)
set @DistinctMam = cast(substring(cast(@sumSt as varchar(50)),7,1) as smallint)
if @DistinctMam = 0  --hn 8703b1
  set @DistinctMam = cast(substring(cast(@sumSt as varchar(50)),11,1) as smallint)
if @DistinctMor = 0 
  set @DistinctMor = 1
if @DistinctMam = 0 
  set @DistinctMam = 1
  
Return  
	cast(@sumSt as varchar(50)) +'a'+ cast( @sumDur as varchar(50)) +'b'+
	case when @Status =',12,13,16,17,20,21,' then cast( @sumMor as varchar(50)) else cast( @sumMor/@DistinctMor as varchar(50)) end
	+'c'+ cast(@sumMam/@DistinctMam as varchar(50)) +'d'

END


GO

