Link to home
Start Free TrialLog in
Avatar of pvsbandi
pvsbandiFlag for United States of America

asked on

Not Returning any data when dates are equal.

Hi,
  i have a Stored Procedure,which takes two date parameters and returns values.
  Part of my procedure is :
   DECLARE @startdate datetime
DECLARE @enddate datetime

SELECT prmykey,min(seqnum) min_seq
 INTO #min_seqnum from audit
WHERE prmytable = 'secmaster'
 AND    code = 1
 AND    name = 'Security'
 AND entrydate Between @startdate and  @enddate

When i execute the procedure with different @startdate and @enddate,it works fine.
 But when i give the same date to both @startdate and @enddate it is not returning any data.
 Example EXEC my_proc '1/1/2006','1/1/2006' doesnot return any data,even tough there is data for that date.
 did anybody encounter something like this,or know the solution?
 Please let me know.
Thanks!
ASKER CERTIFIED SOLUTION
Avatar of bret
bret
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pvsbandi

ASKER

Oh yeah,
   i completely forgot about the timestamp.Thanks Bret.
   But,if i truncate the timestamp,it'll bring me wrong results for the "different" @startdate and @enddate.
   Can you suggest a way to handle both when they are equal and when they are different?

Thanks!
>>You should otherwise be selecting between "1/1/2006" and "1/1/2006 23:59:59:999"

Bret,i give these dates (@startdate and @enddate) as prompts to the users.So,they are expected to enter simply the
 dates without timestamps.My reporting tool is Business Objects.It automatically takes the dates with the timestamp
 00.00.00.
Perhaps use dateadd() to add 1 day and then subtract 1ms from @enddate?
What i want to do is to handle 2 cases :
 Case1 : When the Date prompts are Different : keep the timestamps intact.
 Case2 : when the Date Prompts are equal : truncate the timestamp to compare only dates 'without timestamps".
 Can you suggest me an idea to incorporate these two in a single query?

Thanks much for your help!!
Hi, I usually don't use BETWEEN when dealing with dates, but use < and >= instead. And I usually add one day to @enddate.

So, when my user insert @startdate = '2006/1/1' and @enddate = '2006/1/1' I'll transform it to:

...
  and entrydate >= @startdate
  and entrydate < dateadd( day, 1, @enddate )

this will show all the records of '2006/1/1' regardless of time.

But this technique is usable only when you don't expect users to input time part of @enddate.

If you want to allow users to input time part, you can use this:

...
  and entrydate < case when @enddate = convert( datetime, convert( varchar(10), @endate, 102 )) then @endate else dateadd( day, 1, @endate ) end

this will add a day only if there is no time part of @enddate. However, this is little nonconsistent approach and can be confusing for some users.
Thanks guys for your help!!