Created
November 29, 2013 14:50
-
-
Save gepser/7706803 to your computer and use it in GitHub Desktop.
Calcula el tiempo laboral entre dos fechas de un empleado.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set ANSI_NULLS ON | |
set QUOTED_IDENTIFIER ON | |
GO | |
ALTER FUNCTION [dbo].[Segundos_Laborales_Extremos] ( @empleado int, @fecha_inicial DATETIME, @fecha_final DATETIME) | |
RETURNS INT | |
AS | |
BEGIN | |
DECLARE @hora_inicio DATETIME, @hora_fin DATETIME | |
DECLARE @segundos_laborales INT, @mismo_dia INT | |
DECLARE @hora_inicio_parametro VARCHAR(10), @hora_fin_parametro VARCHAR(10) | |
SET @hora_inicio_parametro = CAST(DATEPART(hour,@fecha_inicial) AS VARCHAR(10)) + ':' +CAST(DATEPART(minute,@fecha_inicial) AS VARCHAR(10)) + ':' + CAST(DATEPART(second,@fecha_inicial) AS VARCHAR(10)) | |
SET @hora_fin_parametro = CAST(DATEPART(hour,@fecha_final) AS VARCHAR(10)) + ':' + CAST(DATEPART(minute,@fecha_final) AS VARCHAR(10)) + ':' + CAST(DATEPART(second,@fecha_final) AS VARCHAR(10)) | |
SET @segundos_laborales = 0 | |
SET @mismo_dia = DATEDIFF(day,@fecha_inicial,@fecha_final) --0=el mismo dia|>0=dias diferentes|<0=error | |
IF @mismo_dia = 0 | |
BEGIN | |
DECLARE num_horario CURSOR FOR | |
select hora_inicio, hora_fin | |
from ad_horario | |
where alcance = 4 | |
and empleado = @empleado | |
and dia = DATEPART(dw,@fecha_inicial) | |
order by hora_inicio | |
OPEN num_horario | |
FETCH NEXT FROM num_horario INTO @hora_inicio, @hora_fin | |
WHILE @@FETCH_STATUS = 0 | |
BEGIN | |
IF DATEDIFF(s,@hora_fin_parametro,@hora_inicio) >= 0 | |
BEGIN | |
SET @segundos_laborales = @segundos_laborales | |
END | |
ELSE IF DATEDIFF(s,@hora_inicio_parametro,@hora_fin) <= 0 | |
BEGIN | |
SET @segundos_laborales = @segundos_laborales | |
END | |
ELSE IF DATEDIFF(s,@hora_inicio_parametro,@hora_inicio) >= 0 AND DATEDIFF(s,@hora_fin_parametro,@hora_inicio) <= 0 AND DATEDIFF(s,@hora_fin_parametro,@hora_fin) >= 0 | |
BEGIN | |
SET @segundos_laborales = @segundos_laborales + DATEDIFF(s,@hora_inicio,@hora_fin_parametro) | |
END | |
ELSE IF DATEDIFF(s,@hora_inicio_parametro,@hora_inicio) >= 0 AND DATEDIFF(s,@hora_fin_parametro,@hora_fin) <= 0 | |
BEGIN | |
SET @segundos_laborales = @segundos_laborales + DATEDIFF(s,@hora_inicio,@hora_fin) | |
END | |
ELSE IF DATEDIFF(s,@hora_inicio_parametro,@hora_inicio) <= 0 AND DATEDIFF(s,@hora_fin_parametro,@hora_fin) >= 0 | |
BEGIN | |
SET @segundos_laborales = @segundos_laborales + DATEDIFF(s,@hora_inicio_parametro,@hora_fin_parametro) | |
END | |
ELSE IF DATEDIFF(s,@hora_inicio_parametro,@hora_inicio) <= 0 AND DATEDIFF(s,@hora_inicio_parametro,@hora_fin) >= 0 AND DATEDIFF(s,@hora_fin_parametro,@hora_fin) <= 0 | |
BEGIN | |
SET @segundos_laborales = @segundos_laborales + DATEDIFF(s,@hora_inicio_parametro,@hora_fin) | |
END | |
FETCH NEXT FROM num_horario INTO @hora_inicio, @hora_fin | |
END | |
CLOSE num_horario | |
DEALLOCATE num_horario | |
END | |
/*SON DIAS DIFERENTES*/ | |
ELSE IF @mismo_dia > 0 | |
BEGIN | |
/*PRIMER DIA*/ | |
DECLARE num_horario CURSOR FOR | |
select hora_inicio, hora_fin | |
from ad_horario | |
where alcance = 4 | |
and empleado = @empleado | |
and dia = DATEPART(dw,@fecha_inicial) | |
order by hora_inicio | |
OPEN num_horario | |
FETCH NEXT FROM num_horario INTO @hora_inicio, @hora_fin | |
WHILE @@FETCH_STATUS = 0 | |
BEGIN | |
IF DATEDIFF(s,@hora_inicio_parametro,@hora_inicio) > 0 | |
BEGIN | |
SET @segundos_laborales = @segundos_laborales + DATEDIFF(s,@hora_inicio,@hora_fin) | |
END | |
ELSE IF DATEDIFF(s,@hora_inicio_parametro,@hora_inicio) <= 0 AND DATEDIFF(s,@hora_inicio_parametro,@hora_fin) >= 0 | |
BEGIN | |
SET @segundos_laborales = @segundos_laborales + DATEDIFF(s,@hora_inicio_parametro,@hora_fin) | |
END | |
ELSE IF DATEDIFF(s,@hora_inicio_parametro,@hora_fin) < 0 | |
BEGIN | |
SET @segundos_laborales = @segundos_laborales | |
END | |
FETCH NEXT FROM num_horario INTO @hora_inicio, @hora_fin | |
END | |
CLOSE num_horario | |
DEALLOCATE num_horario | |
/*ULTIMO DIA*/ | |
DECLARE num_horario CURSOR FOR | |
select hora_inicio, hora_fin | |
from ad_horario | |
where alcance = 4 | |
and empleado = @empleado | |
and dia = DATEPART(dw,@fecha_final) | |
order by hora_inicio | |
OPEN num_horario | |
FETCH NEXT FROM num_horario INTO @hora_inicio, @hora_fin | |
WHILE @@FETCH_STATUS = 0 | |
BEGIN | |
IF DATEDIFF(s,@hora_fin_parametro,@hora_fin) <= 0 | |
BEGIN | |
SET @segundos_laborales = @segundos_laborales + DATEDIFF(s,@hora_inicio,@hora_fin) | |
END | |
ELSE IF DATEDIFF(s,@hora_fin_parametro,@hora_fin) >= 0 AND DATEDIFF(s,@hora_fin_parametro,@hora_inicio) <= 0 | |
BEGIN | |
SET @segundos_laborales = @segundos_laborales + DATEDIFF(s,@hora_inicio,@hora_fin_parametro) | |
END | |
ELSE IF DATEDIFF(s,@hora_fin_parametro,@hora_inicio) >= 0 | |
BEGIN | |
SET @segundos_laborales = @segundos_laborales | |
END | |
FETCH NEXT FROM num_horario INTO @hora_inicio, @hora_fin | |
END | |
CLOSE num_horario | |
DEALLOCATE num_horario | |
END | |
RETURN @segundos_laborales | |
END | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, @dias_diferencia INT, @contador INT | |
DECLARE @horarios TABLE (dia INT,segundos INT) | |
INSERT INTO @horarios(dia,segundos) | |
SELECT dia,sum(datediff(s,hora_inicio,hora_fin)) as segundos | |
FROM ad_horario | |
WHERE alcance = 4 | |
AND empleado = @empleado | |
GROUP BY dia | |
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 + IsNull((select segundos from @horarios where dia = datepart(dw,@fecha_variable)),0) | |
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