且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

sql server中两个日期之间的月份,sql server中每个日期的开始和结束日期

更新时间:2023-01-20 11:24:51

一种方法是递归CTE:

One method is a recursive CTE:

with cte as (
      select dateadd(day, 1 - day(@startdate), @startdate) as som,
             eomonth(@startdate) as eom
      union all
      select dateadd(month, 1, som), eomonth(dateadd(month, 1, som))
      from cte
      where dateadd(month, 1, som) < @enddate
     )
select *
from cte;

如果要输入月份名称,则可以使用 datename(个月)。

If you want the name of the month, then you can use datename(month, som).