Search Query Problem

S

Simon Gare

Hi,

have a search.asp page with results.asp page drawing data from an SQL db,
problem is the user has to type the whole field value into the search box to
retrieve the value on results.asp, what I need is to type in just a few
characters e.g. at the moment to search for all pickups at Heathrow Terminal
1 the user has to type in

Heathrow Terminal 1

When really I just want them to type in

Heathrow or even Heath etc.

Query below

SELECT * FROM booking_form WHERE AirportStation LIKE '" +
Replace(BookingForm__varAirportStation, "'", "''") + "'


anyone help?

Simon
--
Simon Gare
The Gare Group Limited

website: www.thegaregroup.co.uk
website: www.privatehiresolutions.co.uk
 
M

michal

SELECT * FROM booking_form WHERE AirportStation LIKE ('%" +
Replace(BookingForm__varAirportStation, "'", "''") + "%')"

should do it
 
E

Evertjan.

Simon Gare wrote on 02 mei 2007 in
microsoft.public.inetserver.asp.general:
Hi,

have a search.asp page with results.asp page drawing data from an SQL
db, problem is the user has to type the whole field value into the
search box to retrieve the value on results.asp, what I need is to
type in just a few characters e.g. at the moment to search for all
pickups at Heathrow Terminal 1 the user has to type in

Heathrow Terminal 1

When really I just want them to type in

Heathrow or even Heath etc.

Query below

SELECT * FROM booking_form WHERE AirportStation LIKE '" +
Replace(BookingForm__varAirportStation, "'", "''") + "'

With "LIKE" you only would have to add a wildcard character.

SELECT * FROM booking_form WHERE AirportStation LIKE '" +_
yourSearchInput + "%" + "'

=============

"Heathrow Te" + "%"

will select:

Heathrow Terminal 1
Heathrow Terminal 2
Heathrow Terminal 3
Heathrow Terminal 4
Heathrow Terminal 5 [?]
 
S

Simon Gare

Just came across a problem, when searching the date field it returns
nothing, current entry format is

dd/mm/yyyy hh:mm:ss
02/05/2007 10:26:00

when I search for 02/05/2007 nothing is returned even though I have the % in
place.

All other search criteria is working ok?

Simon
 
M

michal

i am not sure but i think that like is not working on data columns ...
you should be fine with
WHERE datCol = '02/05/2007'
 
S

Simon Gare

but we retrieve the value from a querystring that is entered on a search
page

BookingForm__varID = Request.Querystring DATE2

TimeOfBooking LIKE '%" + Replace(BookingForm__varID, "'", "''") + "%'

but returns no values doesn't matter what format you put it in either, will
retrieve values when the format is correct though which is no good to us.

02/05/2007 11:02:35 etc
 
M

Mike Brind

Michal is correct. LIKE doesn't work with datetime. You will have to use
Between to find datetimes in a range. If you want to find items with
today's date, your range will be from 02/05/2007 00:00:00 to 02/05/2007
23:59:59.
 
S

Simon Gare

ok, so if I want to find all entries an the results page with the date
specified in the querystring which is entered manually by the user on the
search page how do I go about that? do I use 2 BETWEEN statements ie

Select * FROM BookingForm WHERE DATE2 BETWEEN Request.Querystring("DATE2")
and BETWEEN Request.Querystring("DATE2")

something like that?

Simon
 
M

Mike Brind

If the likely value of Request.QueryString("yourdate") is eg 02/05/2007,
then the query would be

Select * From BookingForm Where datefield Between '" &
Request.QueryString("yourdate") & " 00:00:00' AND '" &
Request.QueryString("yourdate") & " 23:59:59'
 
S

Simon Gare

Thanks Mike, works ok when looking for a date but upsets the other search
criteria, SQL Query looks at 1 value in the querystring and compares across
many fields


SELECT * FROM booking_form WHERE ID LIKE '" + Replace(BookingForm__varID,
"'", "''") + "' OR TimeOfBooking BETWEEN '" + Replace(BookingForm__varID,
"'", "''") + " 00:00:00' AND '" + Replace(BookingForm__varID, "'", "''") + "
23:59:59' OR PAX_NAME1 LIKE '%" + Replace(BookingForm__varID, "'", "''") +
"%' OR COLL_STREET LIKE '%" + Replace(BookingForm__varID, "'", "''") + "%'
OR COLL_POST_CODE LIKE '%" + Replace(BookingForm__varID, "'", "''") + "%' OR
MOBILE_NO LIKE '%" + Replace(BookingForm__varID, "'", "''") + "%' OR
COLL_BUILDING_NAME_NO LIKE '%" + Replace(BookingForm__varID, "'", "''") +
"%' ORDER BY TimeOfBooking DESC"


When i put in the date it works fine but if i put in a name which looks in
PAX_NAME1 field the error reads

Syntax error converting datetime from character string.


Any ideas?

Simon
 
M

Mike Brind

Deary me. Just a glance at that SQL string makes me faint.

Try taking all those replace operations out of the concatention. Do them
before you build your SQL string. Better still, use parameter markers or
stored procedures.

Mike
 
B

Bob Barrows [MVP]

Simon said:
Thanks Mike, works ok when looking for a date but upsets the other
search criteria, SQL Query looks at 1 value in the querystring and
compares across many fields
Well, forget this idea. it cannot work. You cannot search a column with one
datatype using a value with an incompatible datatype. You will have to look
at the value supplied by the user and decide which columns to include in the
WHERE clause.
Personally, I would provide the user a means of indicating what type of data
is being supplied (a dropdown, or some radio buttons). In the responding
page, first validate that the data supplied for the search can be coerced
into the selected data type, returning an error to the user if it cannot be.
Once you have determined that the user has supplied compatible data and data
type, build your WHERE clause only including the columns that are compatible
with the selected data type.
 
M

Mike Brind

Bob Barrows said:
Well, forget this idea. it cannot work. You cannot search a column with
one datatype using a value with an incompatible datatype.

I was so distracted by all the Replace() operations going on, I missed the
fact that Simon was using the same value for all the filters.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top