473,395 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Build SQL search string

Hi There

I am creating a search page and need help writing the code to build the sql
string.

I have 3 fields on the main page which a user can choose to enter search
terms in any of the 3 fields. My question is how do I write the logic so it
only includes the fields in the search criteria where the user has entered
anything.

Fields are

Production No
Production Title
Synopsis

The user may enter any, all or some of the search teams in the fields.

On the search page I am using lots of if conditions to build my sql string
like

strSQL = "Select * from Productions where "

now after where there are lots of permutations and combination like
production no could be empty or it could be filled in and the other fields
might be empty or filled in as well.

Does someone has a simple logic to build the correct sql String?

All fields are text.

Thanks
Jul 19 '05 #1
4 10519
CJM
A simple solution would be:

sSQL = "Select * from Productions where ProductNo like '%" & sProductNo &
"%'," & _
"ProductTitle like '%" & sProductTitle & "%'," & _
"Synopsis like '%" & sSynopsis & "%',"

I havent tested it, and it also depends on if you are allowing fuzzy
searches (using LIKE) rather than exact searches. I've assumed fuzzy
searching.

Chris

"JP SIngh" <no**@none.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
Hi There

I am creating a search page and need help writing the code to build the sql string.

I have 3 fields on the main page which a user can choose to enter search
terms in any of the 3 fields. My question is how do I write the logic so it only includes the fields in the search criteria where the user has entered
anything.

Fields are

Production No
Production Title
Synopsis

The user may enter any, all or some of the search teams in the fields.

On the search page I am using lots of if conditions to build my sql string
like

strSQL = "Select * from Productions where "

now after where there are lots of permutations and combination like
production no could be empty or it could be filled in and the other fields
might be empty or filled in as well.

Does someone has a simple logic to build the correct sql String?

All fields are text.

Thanks

Jul 19 '05 #2
Hi,

First of all I think it is a bad idea to use "SELECT *" as this will return
more data then you are going to consume on the web page. The client (in
this case your web page) should only ever select data that it is going to
use. This is good practice.

However I would do something like this

strSQL = "SELECT Field1, Field2, Field3 "
strSQL = strSQL & "FROM Productions "
strSQL = strSQL & "WHERE (1 = 1) "
If Not strProductionNo = "" Then strSQL = strSQL & "AND (ProductionNo = '" &
strProductionNo & "') "
If Not strProductionTitle = "" Then strSQL = strSQL & "AND (ProductionTitle
LIKE '%" & strProductionTitle & "%') "
If Not strSynopsis = "" Then strSQL = strSQL & "AND (Synopsis LIKE '%" &
strSynopsis & "%') "
strSQL = strSQL & "ORDER BY Field1"

This way the user can fill in as many of the search fields as he or she
wishes in order to narrow down the results. You need part of your WHERE
clause to be unconditional so I often use (1=1) because this never
eliminates any rows. Realize however if the user does not put in any search
criteria that all the rows in the table will be returned. For this reason
you may wish to put some kind of logic in place that will not perform the
search if all the fields are empty. Also, particularly when doing search
engines, it is a good idea to use a TOP clause to restrict the maximum
number of results. You should not rely on the underlying table/view to have
a reasonable number of rows. It may have 100's of thousands of rows. No
one really wants to scroll through 500 results from their search engine
anyway. Using a TOP 500 statement is a good idea to reduce overhead on your
server and if the user gets that many results back it's time to narrow the
search down anyway.

Cheers,

Ken.

"JP SIngh" <no**@none.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
Hi There

I am creating a search page and need help writing the code to build the sql string.

I have 3 fields on the main page which a user can choose to enter search
terms in any of the 3 fields. My question is how do I write the logic so it only includes the fields in the search criteria where the user has entered
anything.

Fields are

Production No
Production Title
Synopsis

The user may enter any, all or some of the search teams in the fields.

On the search page I am using lots of if conditions to build my sql string
like

strSQL = "Select * from Productions where "

now after where there are lots of permutations and combination like
production no could be empty or it could be filled in and the other fields
might be empty or filled in as well.

Does someone has a simple logic to build the correct sql String?

All fields are text.

Thanks

Jul 19 '05 #3
What happens incase used did not enter anything in the second and third
fields?
"CJM" <cj*****@yahoo.co.uk> wrote in message
news:Ok****************@TK2MSFTNGP11.phx.gbl...
A simple solution would be:

sSQL = "Select * from Productions where ProductNo like '%" & sProductNo &
"%'," & _
"ProductTitle like '%" & sProductTitle & "%'," & _
"Synopsis like '%" & sSynopsis & "%',"

I havent tested it, and it also depends on if you are allowing fuzzy
searches (using LIKE) rather than exact searches. I've assumed fuzzy
searching.

Chris

"JP SIngh" <no**@none.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
Hi There

I am creating a search page and need help writing the code to build the

sql
string.

I have 3 fields on the main page which a user can choose to enter search
terms in any of the 3 fields. My question is how do I write the logic so

it
only includes the fields in the search criteria where the user has entered anything.

Fields are

Production No
Production Title
Synopsis

The user may enter any, all or some of the search teams in the fields.

On the search page I am using lots of if conditions to build my sql string like

strSQL = "Select * from Productions where "

now after where there are lots of permutations and combination like
production no could be empty or it could be filled in and the other fields might be empty or filled in as well.

Does someone has a simple logic to build the correct sql String?

All fields are text.

Thanks


Jul 19 '05 #4
CJM
If the user doesnt enter a value for any of the fields, the query will
search for '%%', which is equivalent to '%', which clearly will select all
values...

I would also point you in the direction of Ken's suggestion. I was going to
suggest that solution but apathy got the better of me(!).

His is different in that his solution does extra work on the web server, by
building a more efficient SQL statement. This solution, minimises the work
done in ASP, but makes the DB Server do a bit more work.

In all but the most demanding environments, it probably wont make a blind
bit of difference!

Chris

"JP SIngh" <no**@none.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
What happens incase used did not enter anything in the second and third
fields?

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Rob Young | last post by:
This is the latest in the "Total Non-Programmer" series. Any feedback on the usability of this tutorial would be greatly appreciated. Thanks, Rob How to Build A Web Database (Without...
10
by: Douglas Buchanan | last post by:
I am using the following code instead of a very lengthly select case statement. (I have a lot of lookup tables in a settings form that are selected from a ListBox. The data adapters are given a...
9
by: Harry | last post by:
Hi there, does anyone know how I can build a regular expression e.g. for the string.search() function on runtime, depending on the content of variables? Should be something like this: var...
2
by: Howard | last post by:
How do I do this with parameterized query? without parameterized query: string search = "hello world search"; search = search.Replace(" ", " AND "); SELECT * FROM TABLE1 WHERE TEXT LIKE '%' +...
1
by: David Alloza | last post by:
Hello, I have exactly the same problem as this guy, but in the case of a ( final ) commercial version of .NET2005. ...
1
by: Mike | last post by:
Hi, I'm a complete newbie to XML and am needing to rush through a job requiring far more knowledge than I have the time to gather. Basically... I need to build a breadcrumb menu for our...
4
by: nmrcarl | last post by:
I'm trying to upgrade a large project from VS 6.0 to VS 2005. After fixing a lot of things that changed (mostly sloppy coding in the original project that VS2005 didn't allow), I got the release...
12
by: DFS | last post by:
I need to scan various network folders and capture all the filenames and create dates. ======================================================= 1st effort used the typical drive scan cFile =...
1
by: durumdara | last post by:
Hi! I have a pager component. This component is makes database slices (record sets) and it is use query string's PageIndex element to identify the actual page. This component is makes pager...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.