Sql date range Issue

  • Hi am passing Startdate and EndDate as an input parameter in my stored procedure.

    How do I work out the scenario where say the sartdate is passed as NULL and the enddate is passed some value. So based on which I would fetch the record from table where the DateField column data is less then the end date. The same scenario applies to StartDate parameter as well.

    I have tried something like

    Time_Date >= CONVERT(varchar(10),@startdate,101) and Time_Date <= CONVERT(varchar(10),@enddate,101).

    Any help would be appreciated.

  • Hi,

    set the initial values if dates are null.

    if @startdate is null

    set @startdate='19000101' --dummy start date

    if @enddate is null

    set @enddate=getdate() --current date as end date

    and in where clause add Time_Date between @startdate and @enddate

    hope this will help..

    regards

    Raghavendra N S

  • yes it did..help thanks Raghav 🙂

  • Keep it simple...

    WHERE Time_Date between ISNULL(@startdate,0) and @enddate

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply