tsql: How to retrieve the last date of each month between given date range

前端 未结 9 1789
深忆病人
深忆病人 2020-12-06 22:00

I have two date for example 08/08/2013 and 11/11/2013 and I need last date of each month starting from August to November in a table so that i can iterate over the table to

9条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-06 22:08

    try this the last row(where) is optional for date filtering

    declare @table table  
    (  
    thisdate date  
    )
    
    
    insert into @table values ('12/01/2013'),('05/06/2013'),('04/29/2013'),('02/20/2013')
    select *,DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,thisdate)+1,0))
    LastDay from @table
    where thisdate between 'givendate' and 'givendate'
    

    The Example Below is for all dates

    thisdate    lastday
    2013-12-01  2013-12-31 23:59:59.000
    2013-05-06  2013-05-31 23:59:59.000
    2013-04-29  2013-04-30 23:59:59.000
    2013-02-20  2013-02-28 23:59:59.000
    

提交回复
热议问题