Skip to content

Instantly share code, notes, and snippets.

@icalderond
Last active August 29, 2015 14:16
Show Gist options
  • Save icalderond/f70734d5439a0cff0b7d to your computer and use it in GitHub Desktop.
Save icalderond/f70734d5439a0cff0b7d to your computer and use it in GitHub Desktop.
Calculo de dias de la semana
declare @primer_dia datetime
declare @ultimo_dia DATETIME
DECLARE @ultimo_dia_habil datetime
declare @d varchar(2)
DECLARE @m varchar(2)
DECLARE @a varchar(4)
DECLARE @FE varchar(50)
DECLARE @FI varchar(50)
DECLARE @FN varchar(50)
DECLARE @FcF varchar(50)
DECLARE @FcUdH VARCHAR(50)
--//recupera el dia ctual
set @d=day(dateadd(month, -1, GETDATE()))
if len(@d) = 1 set @d='0' + @d
--//recupera el mes actual
set @m=month(dateadd(month, -1, GETDATE()))
if len(@m)=1 set @m='0' + @m
--//recupera el año actual
set @a=year(dateadd(month, -1, GETDATE()))
--//fecha dada por el sistema
set @FE=@a + @m + @d
--//Primer dia del Mes
set @FI=@a + @m + '01'
--//Ultimo dia del mes
set @FN=dateadd( month,1,@FI) -1
--//Ultimo dia del mes en formato yyyymmdd
--//recupera el dia ctual
set @d=cast(day(@FN)as varchar)
if len(@d) = 1 set @d='0' + @d
--//recupera el mes actual
set @m=cast(month(@FN)as varchar)
if len(@m)=1 set @m='0' + @m
set @FcF=cast(year(@FN)as varchar) + @m + @d
set @primer_dia = @FI + ' 00:00:00.000'
set @ultimo_dia = @FcF + ' 23:59:59.000'
-- Forzamos el primer día de la semana a Lunes, para calcular el último día habil del mes.
SET DATEFIRST 1
SET @ultimo_dia_habil=@ultimo_dia
WHILE (DATEPART(dw,@ultimo_dia_habil)) > 5
BEGIN
SET @ultimo_dia_habil = @ultimo_dia_habil -- 1
END
SELECT @primer_dia, @ultimo_dia, @ultimo_dia_habil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment