Jump to content
  • Revista PROGRAMAR: Já está disponível a edição #60 da revista programar. Faz já o download aqui!

Sign in to follow this  
jafm

Segunda e domingos de cada Mes

Recommended Posts

jafm

Boas,

Estou a criar um ciclo em sql que me devolva o intervalo de datas de segunda feira a domingo de cada mês, para isso estou a utilizar o seguinte ciclo, mas ele está a devolver de domingo a sábado, o que preciso da fazer para que ele me devolva de segunda a domingo?


drop table #tmp_datesforMonth
go
declare @begDate datetime = '2016-04-01'
declare @endDate datetime = '2016-04-30';
WITH N(n) AS 
(   SELECT 0 
    UNION ALL
   SELECT n+1
   FROM N
   WHERE n <= datepart(dd,@enddate)
)
SELECT	 DATEADD(dd,n,@BegDate) as dDate
into #tmp_datesforMonth
FROM	    N
WHERE	   MONTH(DATEADD(dd,n,@BegDate)) = MONTH(@BegDate)
select 
MIN(dDate) as segunda,
Max(dDate) as domingo
, datediff(week, dateadd(week, datediff(week, 0, dateadd(month, datediff(month, 0, dDate), 0)), 0), dDate) as WeekNumForMonth
from #tmp_datesforMonth
--where datediff(week, dateadd(week, datediff(week, 0, dateadd(month, datediff(month, 0, dDate), 0)), 0), dDate)=3
group by datediff(week, dateadd(week, datediff(week, 0, dateadd(month, datediff(month, 0, dDate), 0)), 0), dDate)
order by 3, 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.