Months between two dates

前端 未结 8 2042
悲&欢浪女
悲&欢浪女 2020-11-28 07:35

Is it possible to get month names between two dates in SQl

ie, 2011-05-01 And 2011-08-01 are the inputs I just w

相关标签:
8条回答
  • 2020-11-28 08:22

    You can do this with a recursive CTE, by building up a table of dates, and getting the month name from each:

    declare @start DATE = '2011-05-01'
    declare @end DATE = '2011-08-01'
    
    ;with months (date)
    AS
    (
        SELECT @start
        UNION ALL
        SELECT DATEADD(month,1,date)
        from months
        where DATEADD(month,1,date)<=@end
    )
    select Datename(month,date) from months
    
    0 讨论(0)
  • 2020-11-28 08:24

    Try this:

    declare 
           @sd date=getdate(),
           @ld date='2016-01-01'
    
    select 
         Datename(month,dateadd(month,number,GETDATE())), 
         number
    from master.dbo.spt_values 
    where type='p' 
         and dateadd(month,number,GETDATE()) <= @ld
    
    0 讨论(0)
提交回复
热议问题