Skip to content

Instantly share code, notes, and snippets.

@gepser
Created November 28, 2013 16:16
Show Gist options
  • Save gepser/7694367 to your computer and use it in GitHub Desktop.
Save gepser/7694367 to your computer and use it in GitHub Desktop.
La función obtiene el tiempo laboral en segundos de un empleado entre dos fechas. Usa las sub funciones https://gist.github.com/gepser/7694301 y https://gist.github.com/gepser/7694205.
/*
La función obtiene el tiempo laboral en segundos de un empleado entre dos fechas.
Esta función trabaja sobre una tabla de horarios por empleado donde cada empleado puede tener muchos horarios por día.
Se asume que no hay traslapes de horarios.
Un ejemplo de la tabla podría ser así:
dia|hora_inicio|hora_fin|empleado
2 |08:00 |13:00 |15
2 |14:00 |18:00 |15
3 |08:00 |13:00 |15
3 |14:00 |18:00 |15
4 |07:00 |13:00 |15 --Miércoles sólo medio día
5 |08:00 |13:00 |15
5 |14:00 |18:00 |15
6 |08:00 |13:00 |15
6 |14:00 |18:00 |15
*/
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[Tiempo_Laboral] ( @FechaInicial datetime, @FechaFinal datetime, @empleado int)
RETURNS FLOAT
AS
BEGIN
DECLARE @Fecha_variable DATETIME
DECLARE @segundos_laborales INT
DECLARE @dias_diferencia INT, @contador INT
SET @segundos_laborales = 0
SET @dias_diferencia = DATEDIFF(d,@FechaInicial,@FechaFinal)
SET @contador = 1
SET @Fecha_variable = @FechaInicial
WHILE @contador < @dias_diferencia
BEGIN
SET @Fecha_variable = dateadd(day,1,@Fecha_variable)
SET @segundos_laborales = @segundos_laborales + dbo.Segundos_Laborales_Dia(datepart(dw,@Fecha_variable),@empleado)
SET @contador = @contador + 1
END
SET @segundos_laborales = @segundos_laborales + dbo.Segundos_Laborales_Extremos(@empleado,@FechaInicial,@FechaFinal)
RETURN @segundos_laborales
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment