Search code examples
sql-serversql-server-2008stored-proceduressql-server-2012procedure

Condition to COMPARE TWO DATE From parameter and ADD if it Satisfies


This is MY procedure , Im getting from and to date in the procedure i have to check if difference between FROM and TO date is greater than 2

if the condition satisfies i have to add 2days in from days and set it as TODATE

ALTER PROCEDURE [dbo].[sp_TU_AvgStdDev_Report] 
@FromDate as Datetime,
@ToDate as Datetime,
@RecipeCode as Varchar(8000),
@Grade as Varchar(10),
@WcID as Int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
SET NOCOUNT ON;
IF (condition to check if @FromDate - @ToDate  > 2)
--if it satisfies
SET @ToDate to @fromDate+2Days

Please help me in figuring it out...


Solution

  • You need to used date functions for this.

    ALTER PROCEDURE [dbo].[sp_TU_AvgStdDev_Report] 
    @FromDate as Datetime,
    @ToDate as Datetime,
    @RecipeCode as Varchar(8000),
    @Grade as Varchar(10),
    @WcID as Int
    AS
    BEGIN
    IF (datediff(day,@FromDate,@ToDate)>2)
    SET @ToDate =  DATEADD(day,2, @fromDate)