ALTER FUNCTION [dbo].[fn_CALC_DT_EMPLOYEE_TOTALCONTRACTEDHOURS](	@PIT datetime
	,@REP_START datetime
	,@REP_END datetime
	,@SecGroupID int
	,@SecUserID int
)
RETURNS TABLE AS RETURN (
	SELECT RT.Ndx, RT.Entity_Id
	, (SELECT ISNULL(SUM(CAST(DTC.Hours as numeric(5,2))),0) 
FROM DT_CONTRACT DTC
INNER JOIN LNK_EMPLOYEE_CONTRACT LEC
ON LEC.Left_Join = RT.Entity_Id
AND LEC.Right_Join = DTC.Entity_Id
AND CONVERT(varchar, @PIT, 106) BETWEEN DTC.Entity_Start_Date AND COALESCE(DTC.Entity_End_Date, CONVERT(varchar, @PIT, 106))) AS Value

, NULL AS Helper
, 4 AS DataType
 FROM DT_EMPLOYEE RT
)
GO
UPDATE [dbo].[REF_SQLFIELDS] SET [Code]=N'SELECT ISNULL(SUM(CAST(DTC.Hours as numeric(5,2))),0) 
FROM DT_CONTRACT DTC
INNER JOIN LNK_EMPLOYEE_CONTRACT LEC
ON LEC.Left_Join = RT.Entity_Id
AND LEC.Right_Join = DTC.Entity_Id
AND ''[PIT]'' BETWEEN DTC.Entity_Start_Date AND COALESCE(DTC.Entity_End_Date, ''[PIT]'')' 
WHERE [Table_Name] = N'DT_EMPLOYEE' AND [Field_Name] = N'TotalContractedHours' AND [CustomisationID] = 0