September 22, 2011 at 6:04 am
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?
September 22, 2011 at 6:11 am
select *
from tbl
where ISNULL(fromDate,GETDATE())<= GETDATE()
and ISNULL(toDate,GETDATE())>= GETDATE()
would work?
September 22, 2011 at 6:12 am
What are you going to do?
Your answers changes the correct solution.
#1 is if
#2 and most likely is a case statement.
September 22, 2011 at 6:21 am
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
September 22, 2011 at 6:28 am
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?
September 22, 2011 at 6:33 am
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.
September 22, 2011 at 6:36 am
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