Avatar of pvsbandi
pvsbandi
Flag 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!
Sybase Database

Avatar of undefined
Last Comment
pvsbandi

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
bret

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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!
pvsbandi

ASKER
>>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.
bret

Perhaps use dateadd() to add 1 day and then subtract 1ms from @enddate?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
pvsbandi

ASKER
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!!
Jan Franek

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.
pvsbandi

ASKER
Thanks guys for your help!!
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.