构造查询的更好方法

时间:2012-09-14 08:03:26

标签: sql sql-server-2008

输入:

Month, Year, GraceMonth.所有都是整数。

我们需要做什么?

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

您尝试了什么?

e.g。 (我在零件中展示)

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 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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

答案 3 :(得分: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()
)
相关问题