Months between two dates

前端 未结 8 2090
悲&欢浪女
悲&欢浪女 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:21

    Inspired by Jamiec's answer, but fixing issue with with from day bigger then to day:

    declare @start DATE
    declare @end DATE 
    
    SELECT  @start='2011-05-19' , @end='2011-08-15' 
    
    ;with months (date)
    AS
    (
       SELECT DATEADD(DAY,1,EOMONTH(@start,-1))
        UNION ALL
        SELECT DATEADD(month,1,date)
        from months
        where DATEADD(month,1,date) < EOMONTH(@end)
    )
    select Datename(month,date)
    from months
    

提交回复
热议问题