Using WHERE clause with BETWEEN and null date parameters

前端 未结 6 808

One of my WHERE clauses is the following:

AND (DateCreated BETWEEN @DateFrom and @DateTo OR (@DateFrom IS NULL OR @DateTo IS NULL))
6条回答
  •  感动是毒
    2021-01-04 01:21

    Use this where clause

    WHERE  ( DateCreated BETWEEN @DateFrom AND @DateTo )
            OR ( @DateFrom IS NULL
                 AND @DateTo IS NULL )
            OR ( @DateFrom IS NULL
                 AND DateCreated <= @DateTo )
            OR ( @DateTo IS NULL
                 AND DateCreated >= @DateFrom ) 
    

提交回复
热议问题