4

输入 :

Month, Year, GraceMonth.都是整数。

我们需要做什么?

首先需要从月、年、日构造日期(需要从当前日期获取),然后将 GraceMonth 添加到其中。添加 GraceMonth 后,我们显然会得到新的 Date。接下来,从构建的日期开始,我们需要将其与当前日期进行比较。

你试过什么?

例如(我在零件中显示)

DECLARE @Month INT = 11
DECLARE @YEAR INT = 2012
DECLARE @Graceperiod INT = 2 

SELECT 
    [Construct Initial Date] = DATEADD(mm, (@YEAR - 1900) * 12 + @Month - 1 , DAY(GETDATE()) - 1) --construct initial date  
    ,[Add Grace period] =DATEADD(mm,@Graceperiod,DATEADD(mm, (@YEAR - 1900) * 12 + @Month - 1 , DAY(GETDATE()) - 1)) --add grace month
    ,[DateDiff] = DATEDIFF
            (
                DAY,
                DATEADD(mm, (@YEAR - 1900) * 12 + (@Month + @Graceperiod) - 1 , DAY(GETDATE()) - 1),
                GETDATE()
             ) -- datediff

结果

Construct Initial Date        Add Grace period              DateDiff
2012-11-14 00:00:00.000       2013-01-14 00:00:00.000      -122

如果您的答案是正确的,那么您在寻找什么?

除了这个还有什么好的方法吗?不用 Casting 越简洁越好。如果它涉及一些棘手的部分(例如一些棘手的数学计算),请提供解释。

提前致谢。

4

4 回答 4

1

试试这个:

DECLARE @Month INT = 11
DECLARE @YEAR INT = 2012
DECLARE @Graceperiod INT = 2 

SELECT DATEADD(mm,(@month-month(getdate())),DATEADD(year,@year-YEAR(getdate()),getdate())) as InitialDate,
       DATEADD(mm,@Graceperiod,DATEADD(mm,(@month-month(getdate())),DATEADD(year,@year-YEAR(getdate()),getdate()))) as GraceDate,
       DATEDIFF(day,DATEADD(mm,@Graceperiod,DATEADD(mm,(@month-month(getdate())),DATEADD(year,@year-YEAR(getdate()),getdate()))),GETDATE()) as DateDiffs
于 2012-09-14T08:13:26.470 回答
1

试试这个:

我认为这可能不会带来任何性能改进,但会减少您的代码

DECLARE @Month INT = 11
DECLARE @YEAR INT = 2012
DECLARE @Graceperiod INT = 2 

;with cte as   (select DATEADD(mm, (@YEAR - 1900) * 12 + @Month - 1 ,0) as begining_month)
 select DATEADD(dd,DAY(GETDATE()) - 1,begining_month) as [Construct Initial Date],
        DATEADD(mm,@Graceperiod,DATEADD(dd,DAY(GETDATE()) - 1,begining_month)) as [Add Grace period] ,
        DATEDIFF(DAY,DATEADD(mm,@Graceperiod,DATEADD(dd,DAY(GETDATE()) - 1,begining_month)), GETDATE()) as [DateDiff]
 from cte
于 2012-09-14T08:31:11.103 回答
1
DECLARE @Month INT = 11
DECLARE @YEAR INT = 2012
DECLARE @Graceperiod INT = 2 

SELECT 
      [Construct Date] = DATEADD(mm, (@YEAR - 1900) * 12 + @Month - 1 , DAY(GETDATE()) - 1) --construct date
      ,[Add Grace period] =DATEADD(mm,@Graceperiod,DATEADD(mm, (@YEAR - 1900) * 12 + @Month - 1 , DAY(GETDATE()) - 1)) --add grace month
      ,[DateDiff] = DATEDIFF(
                        DAY,
                        DATEADD(mm,@Graceperiod,DATEADD(mm, (@YEAR - 1900) * 12 + @Month - 1 , DAY(GETDATE()) - 1)),
                        GETDATE()
                  ) -- datediff
于 2012-09-18T04:47:49.340 回答
0

你做了三遍同样的计算,试试这个来保存那个

DECLARE @Month INT = 11
DECLARE @YEAR INT = 2012
DECLARE @Graceperiod INT = 2 

-- This is that calculation
DECLARE @ConstructInitialDate DATETIME = DATEADD(mm, (@YEAR - 1900) * 12 + @Month - 1 , DAY(GETDATE()) - 1)

SELECT 
    [Construct Initial Date] = @ConstructInitialDate --construct initial date  
    ,[Add Grace period] =DATEADD(mm, @Graceperiod, @ConstructInitialDate) --add grace month
    ,[DateDiff] = DATEDIFF
(
   DAY,
   DATEADD(mm,@Graceperiod, @ConstructInitialDate),
   GETDATE()
)
于 2012-09-14T08:13:16.443 回答