带有变量年份的固定日期(SQL Server)

时间:2014-10-14 13:37:49

标签: sql-server date declare

我正在努力为去年,今年和今年设定一个固定的日期。明年,例如11月1日。

我可以宣布我的岁月

 declare @lastyear  int;    set @lastyear = year (getdate()-365) 
 declare @thisyear  int;    set @thisyear = year (getdate()) 
 declare @nextyear  int;    set @nextyear = year (getdate()+365)

但我很难知道如何确定每年的固定日期。

我想声明固定的日期和月份,例如

declare @fixeddate; set @fixeddate = CONVERT(VARCHAR(6), '2014.11.01', 107)

然后追加这一年,但我无处可去。

类似的东西:

@nextyear + @fixeddate(作为约会!)

帮助; - )

5 个答案:

答案 0 :(得分:2)

尝试这样的事情:

DECLARE @fixedDate VARCHAR(4) = '1101'
DECLARE @addYears INT = 0 -- Add (+)/(-) values to get future and past years
DECLARE @currentYear VARCHAR(4) = CONVERT(VARCHAR(4), Year(Getdate()) + @addYears)

DECLARE @date DATE = CONVERT(DATE, @currentYear + @fixedDate)

如果您还需要包含time

DECLARE @fixedDate VARCHAR(13) = '1101 23:59:59' --Change the length as VARCHAR(13)
DECLARE @addYears INT = 0 -- Add (+)/(-) values to get future and past years
DECLARE @currentYear VARCHAR(4) = CONVERT(VARCHAR(4), Year(Getdate()) + @addYears)

--Declare @date as DATETIME and function as CONVERT(DATETIME, ...)
DECLARE @date DATETIME = CONVERT(DATETIME, @currentYear + @fixedDate)

答案 1 :(得分:1)

DECLARE @FixedDate DATE = '2014-11-01';
DECLARE @NextYearFixed DATE = DATEADD(year, 1, @FixedDate) -- 2015-11-01
DECLARE @PreviousYearFixed DATE = DATEADD(year, -1, @FixedDate) -- 2013-11-01

答案 2 :(得分:1)

您可以使用DATEADD / DATEDIFF对进行此类工作:

select
  DATEADD(year,DATEDIFF(year,'20010101',GETDATE()),'20011101') as NovThisYear,
  DATEADD(year,DATEDIFF(year,'20010101',GETDATE()),'20001101') as NovLastYear,
  DATEADD(year,DATEDIFF(year,'20010101',GETDATE()),'20021101') as NovNextYear

你只需使用两个日期,它们之间具有恒定的关系,表现出你想要的最终关系(例如,在上面的第二对中,我选择了之前的年11月1日('20001101')< / em>第一个日期('20010101'))

结果:

NovThisYear             NovLastYear             NovNextYear
----------------------- ----------------------- -----------------------
2014-11-01 00:00:00.000 2013-11-01 00:00:00.000 2015-11-01 00:00:00.000

答案 3 :(得分:0)

你可以这样做:

DECLARE
    @lastyear  INT
    ;    
SET 
    @lastyear = DATEPART(YEAR, GETDATE())
    ;
DECLARE 
    @thisyear INT
    ;    
SET
    @thisyear = @lastyear + 1
    ;
DECLARE 
    @nextyear INT
    ;    
SET
    @nextyear = @thisyear + 1
    ;

DECLARE
    @fixeddate DATE
    ; 
SET
    @fixeddate = CAST(CAST(@lastyear AS VARCHAR(4)) + '-' + CAST(11 AS VARCHAR(2)) + '-' + CAST(1 AS VARCHAR(2)) AS DATE)
    ;
SELECT
    @fixeddate
    ;

答案 4 :(得分:0)

全部谢谢!

我最终将所有建议的组合用于宣布最后,本和下一个财政年度(FY)。我们是在11月1日左右,但您可以使用任何日期!

您只需要将@today替换为getdate()广告,然后它就会永远运行!您可以通过添加手动日期来测试它,例如下面的示例...

我被困住的一个人就是看起来最后的日期是例如&#39; 2014.10.31 00:00:00&#39;而不是&#39; 2014.10.31 23:59:59&#39;

您可以使用DATEADD(S,86399,@ lastFYend)选择它,但我需要确保@fixedend包括小时分和秒。

有什么建议吗?

-- when are we now?
Declare @today smalldatetime; set @today = '2014.10.14'   -- for testing, normally set to getdate()

Declare @1NOVthisyear   AS smalldatetime;  Set @1NOVthisyear    = DATEADD(year,DATEDIFF(year,'20010101',@today),'20011101') 
Declare @30Octlastyear  AS smalldatetime;  Set @30Octlastyear   = DATEADD(year,DATEDIFF(year,'20010101',@today),'20001031') 

Declare @addyears INT; Set @addyears = (Select Case when @today >= @1NOVthisyear then 1 when @today < @1NOVthisyear then 0 END )

-- FY start / End dates
DECLARE @fixedstart     VARCHAR(4) = '1101'
DECLARE @fixedend       VARCHAR(4) = '1031'

DECLARE @nextnextYear   VARCHAR(4) = CONVERT(VARCHAR(4), Year(@today)   + @addYears +1)
DECLARE @nextYear       VARCHAR(4) = CONVERT(VARCHAR(4), Year(@today)   + @addYears)
DECLARE @thisYear       VARCHAR(4) = CONVERT(VARCHAR(4), Year(@today)   + (@addYears -1))
DECLARE @lastYear       VARCHAR(4) = CONVERT(VARCHAR(4), Year(@today)   + (@addYears -2))
--starts
DECLARE @nextFYstart    DATEtime = CONVERT(smalldatetime, @nextYear     + @fixedstart)
DECLARE @thisFYstart    DATEtime = CONVERT(smalldatetime, @thisYear     + @fixedstart)
DECLARE @lastFYstart    DATEtime = CONVERT(smalldatetime, @lastYear     + @fixedstart)
--ends
DECLARE @nextFYend      DATEtime = CONVERT(smalldatetime, @nextnextYear + @fixedend)
DECLARE @thisFYend      DATEtime = CONVERT(smalldatetime, @nextYear     + @fixedend)
DECLARE @lastFYend      DATEtime = CONVERT(smalldatetime, @thisYear     + @fixedend)

Select

---- testing
--@lastFYstart                      As 'Last FY start', 
--DATEADD (S, 86399, @lastFYend)        AS 'Last FY End',   
--@thisFYstart                      As 'This FY start',
--DATEADD (S, 86399, @thisFYend)        AS 'This FY End',
--@nextFYstart                      As 'Next FY start',
--DATEADD (S, 86399, @nextFYend)        AS 'Next FY End'