如何生成具有开始月份日期和结束月份日期的日历表 [英] How to generate calendar table having begin month date and end month Date

查看:30
本文介绍了如何生成具有开始月份日期和结束月份日期的日历表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在具有乞讨月份日期和结束月份日期的两个日期之间生成日历表.如果它大于今天,那么它应该在当前日期停止.

I need to generate calendar table between two dates having begging month date and end month date. And if its greater than today, then it should stop at current date.

应该是这样的:

如您所见,Eomonth 列的最后一个值具有今天的日期(不是月末)

As you can see the last value for column Eomonth has today's date (not the end of the month)

谢谢

推荐答案

如果你没有日历表,你可以使用一个临时的理货表

If you don't have a calendar table, you can use an ad-hoc tally table

示例

Declare @Date1 date = '2018-01-01'
Declare @Date2 date = GetDate()

Select [Month] = D
      ,[Eomonth] = case when EOMONTH(D)>@Date2 then convert(date,GetDate()) else EOMONTH(D) end
 From  (
        Select Top (DateDiff(Month,@Date1,@Date2)+1) 
               D=DateAdd(Month,-1+Row_Number() Over (Order By (Select Null)),@Date1) 
         From  master..spt_values n1
       ) A

退货

Month       Eomonth
2018-01-01  2018-01-31
2018-02-01  2018-02-28
2018-03-01  2018-03-31
2018-04-01  2018-04-30
2018-05-01  2018-05-31
2018-06-01  2018-06-30
2018-07-01  2018-07-31
2018-08-01  2018-08-31
2018-09-01  2018-09-30
2018-10-01  2018-10-31
2018-11-01  2018-11-30
2018-12-01  2018-12-31
2019-01-01  2019-01-31
2019-02-01  2019-02-13   <-- Today's date

这篇关于如何生成具有开始月份日期和结束月份日期的日历表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆