• Select @day = max(day1)

    FROM table1

    WHERE date1 = ISNULL( @date, (SELECT max(date1) from table1))

    _____________
    Code for TallyGenerator