Skip to content

Instantly share code, notes, and snippets.

@ebenito
Last active October 5, 2020 09:52
Show Gist options
  • Save ebenito/3b47e6d99565ff3be8e28aecbffcb80d to your computer and use it in GitHub Desktop.
Save ebenito/3b47e6d99565ff3be8e28aecbffcb80d to your computer and use it in GitHub Desktop.
Calcula el primer día deseado de la semana del próximo mes, por ejemplo el primer domingo del próximo mes
/* Calcula el primer día deseado de la semana del próximo mes, por ejemplo el primer domingo del próximo mes:
select primerDomEnProxMes from dbo.fnCalcProxDiaSemEnProxMes(getdate())
*/
CREATE FUNCTION [dbo].[fnCalcProxDiaSemEnProxMes](
@D Datetime
)
RETURNS @output TABLE(primerDomEnProxMes datetime, primerLunEnProxMes datetime, primerMarEnProxMes datetime, primerMieEnProxMes datetime, primerJueEnProxMes datetime, primerVieEnProxMes datetime, primerSabEnProxMes datetime)
BEGIN
Declare @Dom TinyInt Set @Dom = 8
Declare @Dom TinyInt Set @Dom = 7
Declare @Lun TinyInt Set @Lun = 8
Declare @Mar TinyInt Set @Mar = 9
Declare @Mie TinyInt Set @Mie = 10
Declare @Jue TinyInt Set @Jue = 11
Declare @Vie TinyInt Set @Vie = 12
Declare @Sab TinyInt Set @Sab = 13
Declare @FONM DateTime -- primeroEnProxMes
--Set @D = GETDATE()
Set @FONM = DateAdd(Month, 1+DateDiff(Month, 0, @D),0)
insert into @output
Select
DateAdd(day, (@Dom -DatePart(weekday, @FONM))%7, @FONM) primerDomEnProxMes,
DateAdd(day, (@Lun -DatePart(weekday, @FONM))%7, @FONM) primerLunEnProxMes,
DateAdd(day, (@Mar -DatePart(weekday, @FONM))%7, @FONM) primerMarEnProxMes,
DateAdd(day, (@Mie -DatePart(weekday, @FONM))%7, @FONM) primerMieEnProxMes,
DateAdd(day, (@Jue -DatePart(weekday, @FONM))%7, @FONM) primerJueEnProxMes,
DateAdd(day, (@Vie -DatePart(weekday, @FONM))%7, @FONM) primerVieEnProxMes,
DateAdd(day, (@Sab -DatePart(weekday, @FONM))%7, @FONM) primerSabEnProxMes
RETURN
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment