Skip to content

Instantly share code, notes, and snippets.

@NightZpy
Created September 14, 2015 04:03
Show Gist options
  • Save NightZpy/8d464977d04e2bae6122 to your computer and use it in GitHub Desktop.
Save NightZpy/8d464977d04e2bae6122 to your computer and use it in GitHub Desktop.
DELIMITER $$
create FUNCTION CuantosDiasSemana
(
FechaInicio datetime,
FechaFin datetime,
DiaSemana tinyint
)
RETURNS INT
BEGIN
IF @FechaInicio > @FechaFin then
RETURN 0;
END IF;
DECLARE @RangoFechas TABLE(fecha datetime);
DECLARE @Dia datetime;
SET @Dia = @FechaInicio;
while @Dia <= @FechaFin;
BEGIN
INSERT INTO @RangoFechas
SELECT @Dia;
SET @Dia = @Dia + 1;
END
RETURN (
SELECT SUM(CASE WHEN datepart(weekday, fecha) = @DiaSemana THEN 1 ELSE 0 END)
FROM @RangoFechas
)
END
END$$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment