2011-01-20 13 views
9

Obtengo un valor de parámetro de fecha como '4-1-2009' del front-end. Ahora quiero hacerlo como¿Cómo agregar un año a una fecha usando la secuencia de comandos SQL en el servidor SQL?

'4-1-2010' en mi procedimiento almacenado. Lo intento como a continuación.

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 

¿Cómo puedo hacer esto? Por favor dígame.

Saludos, N.SRIRAM

Respuesta

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

DATEADD función Identificación de la solución

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

O

Declare @E DATETIME, 

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

Sir He modificado mi pregunta. Por favor responde. – sriramjitendra

0

que tenían el mismo problema que la persona que hace la pregunta. Las otras respuestas no están abordando su problema.

Lo que tiene son variables y necesita cambiar la fecha en ellas mediante el incremento. Estabas en el camino correcto.

Aquí hay una demostración que puede copiar y pegar en SSMS y simplemente funcionará.

/*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 
Cuestiones relacionadas