2011-01-20 46 views
9

我从前端获取日期参数值为'4-1-2009'。现在我想在我的存储过程中将其作为如何在SQL服务器中使用SQL脚本添加一年到日期?

'4-1-2010'。我想下面。

ALTER PROCEDURE [dbo].[SP_EMP]      

@STARTDATE DATETIME, 

@ENDDATE DATETIME, 

@STARTDATE2 DATETIME, 

SET @STARTDATE2=DATEADD(yy, 1, @STARTDATE) 


AS      
BEGIN 

SELECT EMPNAME FROM EMP WHERE JOINDATE>@STARTDATE2 

----// SOME JOINS //---- 

END 

我该怎么做? 请告诉我。

问候, N.SRIRAM

回答

3
select dateadd(yy, 1, '20 Jan 2011') 
12

DateAdd函数ID解决方案

SELECT DATEADD(year, 1, '4-1-2009') FROM UserLog 

或者

Declare @E DATETIME, 

SET @E=Select DATEADD(year, 1, '4-1-2009') 
+0

先生,我修改了我的问题。请回复。 – sriramjitendra 2011-01-20 07:13:20

0

我有同样的问题,因为问这个问题的人。其他答案没有解决他的问题。

他所拥有的是变量,需要通过递增来改变日期。你在正确的轨道上。

这里是一个演示,你可以复制并粘贴到SSMS,它会工作。

/*First declare your varabiles, you can use date or datetime, or even var only after using dateadd the format will change */ 
Declare @CTCStartDate date 
Declare @CTCEndDate date 

/* Now define your initial values, you may want to have these by a SSRS report or other program */ 
    Set @CTCStartDate = '2015-01-01' 
    Set @CTCEndDate = '2015-11-11' 

    /* See the inital values */ 
    Select @CTCStartDate as InitialStartDT, @CTCEndDate as InitialEndDT 

    /* Increment the year by the number you desire, even this can be a variable */ 
    Set @CTCEndDate = DATEADD(YYYY,1, @CTCEndDate) 
    Set @CTCStartDate = DATEADD(YYYY,1, @CTCStartDate) 

    /* See the final results */ 
    Select @CTCStartDate as StartDT, @CTCEndDate as EndDT