Home All Groups Group Topic Archive Search About

Date between two wildcards

Author
4 Sep 2006 9:50 AM
jochenhebbrecht
I have a webapplication where the administrator can view into a
logbook. To shorten the list of the logbook, he can filter the list by
entering a startdate AND/OR an enddate. If he doesn't fill in the
dates, the list shouldn't be filtered.

So this is what I did for my PreparedStatement

SELECT * from logbook WHERE date >= ? AND date <= ?

If the user fills in nothing, I gave the field ? => "%" as wilcard.
That works perfect for >= but not for <=.

So my question is: how to make the query right if the user specifies no
startdate and no enddate?

( yes, I could use different sql queries, but I just want one! )

Author
4 Sep 2006 10:19 AM
ML
Use the minimum and the maximum allowed values:

SELECT *
  from logbook
  WHERE (date >= '1750-01-01 00:00:00.000')
             AND (date <= '9999-12-31 23:59:59.997')


Are you certain that your current conditions ('%') return expected results?


ML

---
http://milambda.blogspot.com/
Are all your drivers up to date? click for free checkup

Author
4 Sep 2006 10:23 AM
jochenhebbrecht
Yes, they work for ">=", but not otherwise.

But your solution works also, but I don't know if it's really the "way
to go" :-)


ML schreef:

Show quoteHide quote
> Are you certain that your current conditions ('%') return expected results?
Author
4 Sep 2006 10:35 AM
ML
You could also try:

where (<date column> >= @dateParam or @dateParam is null)
  and (<date column> <= @dateParam of @dateParam is null)

....however, the optimizer will most probably end up using a table scan in
this case.

Your best alternative would be to not use the conditions at all when the
@dateParam is not specified.

Perhaps you should do more reading on this subject:
http://www.sommarskog.se/dyn-search.html


ML

---
http://milambda.blogspot.com/
Author
4 Sep 2006 10:31 AM
Erland Sommarskog
ML (M*@discussions.microsoft.com) writes:
> Use the minimum and the maximum allowed values:
>
> SELECT *
>   from logbook
>   WHERE (date >= '1750-01-01 00:00:00.000')
>              AND (date <= '9999-12-31 23:59:59.997')

Small correction: the minimum date is 17530101

> Are you certain that your current conditions ('%') return expected
> results?

Apparently the client API converts % to some date in the past. SQL Server
does not seem to like it.

--
Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
Author
4 Sep 2006 10:39 AM
ML
> Small correction: the minimum date is 17530101

Oh, yes, right. A typo. Thanks for pointing it out.


ML

---
http://milambda.blogspot.com/
Author
4 Sep 2006 4:16 PM
mark
<jochenhebbre***@gmail.com> wrote in message
Show quoteHide quote
news:1157363415.292279.218240@74g2000cwt.googlegroups.com...
>I have a webapplication where the administrator can view into a
> logbook. To shorten the list of the logbook, he can filter the list by
> entering a startdate AND/OR an enddate. If he doesn't fill in the
> dates, the list shouldn't be filtered.
>
> So this is what I did for my PreparedStatement
>
> SELECT * from logbook WHERE date >= ? AND date <= ?
>
> If the user fills in nothing, I gave the field ? => "%" as wilcard.
> That works perfect for >= but not for <=.
>
> So my question is: how to make the query right if the user specifies no
> startdate and no enddate?
>
> ( yes, I could use different sql queries, but I just want one! )

do it clientside ?

if to date is blank then date is 01/01/2005
if from date is blank then date is 01/01/1980
or something
then just between @fromdate and @todate

mark

Bookmark and Share