DateTime Issue

  • Hi all,

    i have two DateTime fields in my table (from and to) which can be null.i want to write a storeprocedure whcih will compare current datetime ...if date time is less that Todate and greater than fromDate then do this..else do this..how i will do?

  • select *

    from tbl

    where ISNULL(fromDate,GETDATE())<= GETDATE()

    and ISNULL(toDate,GETDATE())>= GETDATE()

    would work?

  • What are you going to do?

    Your answers changes the correct solution.

    #1 is if

    #2 and most likely is a case statement.

  • Actually i have banners data in table...each baner have time from...if current time si inside that timeframe..mean from and todate then display otherwise not

  • Please Reply me ASAP

    this works only when i remove any = sign....from <= or >=

    select *

    from BDBanner

    where ISNULL(FromDate,GETDATE())<= GETDATE()

    and ISNULL(ToDate,GETDATE())>= GETDATE()

    what i do now?

  • Well the normal query would look something like this =>

    SELECT * FROM dbo.BannerPlacements WHERE GETDATE() BETWEN StartDateTime AND EndDateTime

    Unfortunately here you don't use defaults which makes this fail so you have to use something like this instead.

    SELECT * FROM dbo.BannerPlacements WHERE GETDATE() BETWEN ISNULL(StartDateTime, '1753-01-01') AND ISNULL(EndDateTime, '9999-12-31')

    Those "default" dates are my personal choices here. You can change those to something else that makes more sense for you.

  • P.S. This is a free "service". We don't work for you and we are not getting paid to help.

    Thank you for your patience and understanding, even if you are stressed.

Viewing 7 posts - 1 through 6 (of 6 total)

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