473,780 Members | 2,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10535
CJM
A simple solution would be:

sSQL = "Select * from Productions where ProductNo like '%" & sProductNo &
"%'," & _
"ProductTit le 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.co m> wrote in message
news:%2******** **********@TK2M SFTNGP10.phx.gb l...
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 strProductionTi tle = "" Then strSQL = strSQL & "AND (ProductionTitl e
LIKE '%" & strProductionTi tle & "%') "
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.co m> wrote in message
news:%2******** **********@TK2M SFTNGP10.phx.gb l...
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******** ********@TK2MSF TNGP11.phx.gbl. ..
A simple solution would be:

sSQL = "Select * from Productions where ProductNo like '%" & sProductNo &
"%'," & _
"ProductTit le 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.co m> wrote in message
news:%2******** **********@TK2M SFTNGP10.phx.gb l...
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.co m> wrote in message
news:%2******** *******@TK2MSFT NGP09.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
5608
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 Programming) This article will teach you step-by-step how to add interactive database-driven capabilities to your existing web site. When the steps
10
2351
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 similar name to the table. Rather than making a long Select Case that could become obsolete if lookup tables are added and the source table of the ListBox is edited I came up with this code.) This code works but of course it gives me build...
9
3154
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 strkey = "something"; var str = "Somethin like this"; if( str.search( / + strkey + / ) > -1 )
2
1478
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 '%' + @search + '%' parameterized query:
1
1759
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. http://216.239.59.104/search?q=cache:FDooiVSC56sJ:myservicescentral.com/site/Alpha-Beta-RC1-RC2-RTM.aspx%3Fpost%3D79%26group%3Dmicrosoft.private.whidbey.framework_sdk+cannot+inherit+from+%27System::Runtime::InteropServices::DispIdAttribute%27&hl=fr I copy/paste his description: "I have both version of Visual Studio...
1
1919
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 website, using a passed URL and an XML menu string, what has been retrieved from an SQL server (we need to support no-script clients, hence the use of the request URL). Essentially I need to do the following::
4
6254
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 version to build successfully. Unfortunately, it crashes right away when I start it. So now I need to build the debug version. Unfortunately, the compiler gives innumerable error messages of the sort shown below. The #include file referenced...
12
2332
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 = Dir(sharedPath) Do While cFile <"" db.Execute("INSERT INTO Table (FileName, blah blah) cFile = Dir
1
3433
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 buttons in HTML to control. Like this: << < 4 5 6 >
0
9636
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10306
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10139
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6727
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.