• This got me a few times in queries where I've used BETWEEN @FromDate AND @ToDate in the WHERE clause, and the data type may be either smalldatetime or datetime. If you don't calculate the end date correctly, you may miss anything on the last day of a quarter where the time is included, or may include the first day of the next quarter, if you don't calculate the end time correctly.

    /* IMPORTANT: Uses 23:59 and NOT 23:59:59, because when compared with smalldatetime,

    which is only accurate to the nearest minute, 23:59 will remain as 23:59,

    but 23:59:59 will be rounded up to midnight on the first day of the the next quarter.

    */

    SET @ToDate = CASE @Quarter

    WHEN 1 THEN CONVERT(datetime,'3/31/'+CONVERT(varchar(4),@Year)+' 23:59')

    WHEN 2 THEN CONVERT(datetime,'6/30/'+CONVERT(varchar(4),@Year)+' 23:59')

    WHEN 3 THEN CONVERT(datetime,'9/30/'+CONVERT(varchar(4),@Year)+' 23:59')

    WHEN 4 THEN CONVERT(datetime,'12/31/'+CONVERT(varchar(4),@Year)+' 23:59')

    END