4

入力:

Month, Year, GraceMonth.すべて整数です。

何をする必要がありますか?

最初に月、年、日から日付を作成し (現在の日付から取得する必要があります)、それに GraceMonth を追加する必要があります。GraceMonth を追加した後、明らかに新しい日付を取得します。次に、構築された日付から、それを現在の日付と比較する必要があります。

何を試しましたか?

例(私は部品で示しています)

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

あなたの答えが正しければ、あなたは何を探していますか?

これ以外に良いアプローチはありますか?キャストなしで簡潔であればあるほど良い。また、難しい部分が含まれている場合は、説明を提供してください (例: 難しい数学的計算など)。

前もって感謝します。

4

4 に答える 4

1

try this:

This may not give any performance improvement i think, but reduces little bit of your code

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 に答える
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 に答える
0

同じ計算を 3 回行っていたので、これを試してみてください

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 に答える