我将根据我写的查询中给出的价值准备接下来的5个月。
DECLARE @StartDate DATETIME = '2022-03-31', @monthadd INT = 5;
; WITH dates AS (
SELECT @StartDate [vade]
UNION ALL
SELECT DATEADD(MONTH,1,[vade])
FROM dates
WHERE DATEADD(MONTH,1,[vade]) <= DATEADD(MONTH,@monthadd,@StartDate)
)
SELECT *
FROM dates
OPTION (MAXRECURSION 0)
GO
但是,当一个月的最后一天为31时,有必要在接下来的几个月中列出最后一天,即最近的一天。我该怎么做?
Actual results
vade |
2022-03-31 00:00:00.000 |
2022-04-30 00:00:00.000 |
2022-05-30 00:00:00.000 |
2022-06-30 00:00:00.000 |
2022-07-30 00:00:00.000 |
2022-08-30 00:00:00.000 |
编辑:
这是一个成熟计划。如果该人在本月的31日分期付款,则必须在每个月的最后一天进行付款。如果他在30日这样做,那么如果有30天,如果有30天,则应该有30天,如果有29天,则应有29天。如果到20开始,则必须是每个月的20个。想象一下,您在本月30日贷款。如果月份为29天,他们将要求您在第29天付款,如果月份为31天,他们会要求您在第30天付款。我知道这很困惑,对此我感到抱歉。
I am preparing the next 5 months date according to the value given in the query I wrote.
DECLARE @StartDate DATETIME = '2022-03-31', @monthadd INT = 5;
; WITH dates AS (
SELECT @StartDate [vade]
UNION ALL
SELECT DATEADD(MONTH,1,[vade])
FROM dates
WHERE DATEADD(MONTH,1,[vade]) <= DATEADD(MONTH,@monthadd,@StartDate)
)
SELECT *
FROM dates
OPTION (MAXRECURSION 0)
GO
However, when the last day of the month is 31, it is necessary to list the last day, which is the nearest day, in the following months. how do i do this?
Actual results
vade |
2022-03-31 00:00:00.000 |
2022-04-30 00:00:00.000 |
2022-05-30 00:00:00.000 |
2022-06-30 00:00:00.000 |
2022-07-30 00:00:00.000 |
2022-08-30 00:00:00.000 |
Edit:
This is a maturity plan. If the person makes installments on the 31st of the month, the payment must be made on the last day of each month. If he does it on the 30th, the month should have 30 if it has 30 days, 30 if it has 31 days, and 29 if it has 29 days. If maturity starts on the 20th, it must be the 20th of each month. Imagine you take out a loan on the 30th of the month. If the month is 29 days, they will ask you to pay on the 29th day, and if the month is 31 days, they will ask you to pay on the 30th day. I know it's very confusing and I'm sorry about that.
发布评论
评论(2)
更新了2022-04-01
如果我正确理解,您想为每个月返回相同的“一天” - @startdate是@startdate是最后一天月。
一种方法是确定@startdate是否是该月的最后一天。如果是这样,请使用 eomonth()在随后的每个月中返回最后一天。否则,请使用dateadd()每月返回指定的“天”。这种方法应适用于任何日期。一种方法如下:
sql:
2022-03-31 (一个月的最后一天)
2022-03-03-30的结果 (不是最后一天)
db&lt; /dbfiddle.uk/?rdbms = sqlServer_2019& fiddle = de5de5da8fe22e53300854d4ad888102f314“ rel =“ nofollow noreferrer”>在这里
Updated 2022-04-01
If I'm understanding correctly, you want to return the same "day" for each month - except when @StartDate is the last day of the month.
One approach would be to determine if the @StartDate is the last day of the month. If so, use EOMONTH() to return the last day in each of the subsequent months. Otherwise, use DATEADD() to return the specified "day" in each month. This approach should work for any date.One approach is as follows:
SQL:
Results for 2022-03-31 (Last Day Of Month)
Results for 2022-03-30 (NOT Last Day Of Month)
db<>fiddle here
/code>函数已经考虑了角案例,例如本月底,因此您无需处理。
为了拥有一个清洁代码,您可以放下 <<代码>存储过程 ,创建(或替换)
dates_list
表,然后在添加到开始日期的几个月中循环。当您需要获得新的日期时,您可以通过设置参数并调用存储过程来刷新该表:
您可以随时使用SQL赋予您的工具随时自由访问该表。
如果您不需要表格以进行更多会话,则可以将表定义为
临时
。 官方文档>详细且全面的示例,请查看以了解更多信息。The
DATEADD
function already takes into account of the corner cases, like the end of the month, so you don't need to handle it.In order to have a cleaner code, you can lay down a
stored procedure
, that creates (or replaces) adates_list
table and then cycles over the number of months to add to the start date.When you need to obtain new dates, you can refresh that table by setting the parameters and calling the stored procedure:
You can freely access the table anytime by using the tools that sql empowers you with.
If you don't need the table to exist for further sessions, you can define the table as
TEMPORARY
. The official documentation on temporary tables is very detailed and comprehensive of examples, check it out to get to know more about it.