|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
search with wildcard - newbie question
Hello all,
I am using coldfusion in conjuction with a sql server database. I am trying to recreate a simple search page based on one given to me by an aquaintance and I am having trouble implementing it. Basically there is a space to type in a job number (name="Job_Number"), then an empty text box to hold a string (name="querystring1"), then two radio buttons, one for "and" (name="querytype" value="and") one for "or" (name="querytype" value="or") and then another empty text box to hold a second string (name="querystring2"). The idea is that a user can search by a job number, or they can search by a keyword, or they can search by two keywords using an 'and' or 'or'. I have these set up on a page called query submitting to a page called queryResults. This is the code I have on my queryResults page: Code:
<cfquery name="q_queryResults" datasource="PLOG">
SELECT *
FROM data_admin.Main_log
WHERE 0=0
<CFIF #FORM.Job_Number# IS NOT "">
AND Job_Number LIKE (#FORM.Job_Number#)
</CFIF>
<CFIF #FORM.querystring1# IS NOT "">
AND ( upper(trim(Refuge_Literal)||' '||trim(Requestor)||' '||trim(Requestor_Office)||' '||trim(Project_Type)||' '||trim(Map_Type)||' '||trim(Rq_Description) ||' '||trim(Assigned_To)) LIKE upper('%#FORM.querystring1#%')
</CFIF>
<CFIF #FORM.querystring2# IS "" AND #FORM.querystring1# IS NOT "">
)
</CFIF>
<CFIF #FORM.querystring2# IS NOT "">
#FORM.querytype# upper(trim(Refuge_Literal)||' '||trim(Requestor)||' '||trim(Requestor_Office)||' '||trim(Project_Type)||' '||trim(Map_Type)||' '||trim(Rq_Description) ||' '||trim(Assigned_To)) LIKE upper('%#FORM.querystring2#%') )
</CFIF>
ORDER BY Date_Assigned DESC, Job_Number DESC
</cfquery>
and then a table that simple displays the results. I get an error message saying Quote:
What am I doing wrong? I would be happy to post more code if that would help. Or if there is a better way to do this I would love to hear it. Thanks anyone, melissa |
|
#2
|
|||
|
|||
|
SQL Server doesn't have a trim() function, it has ltrim() and rtrim() which trim to the left and the right of the string, respectively. So you might try:
upper(rtrim(ltrim(Refuge_Literal)))
__________________
Ask if you have a question, but also help answer questions that you have knowledge of! Thanks, Brian. How to Post a Question in the Forums |
|
#3
|
|||
|
|||
|
Hi kiteless! You always come to my rescue
Does the or function work (||) because after I replaced all the trims I got this error: "[Macromedia][SQLServer JDBC Driver][SQLServer]Line 9: Incorrect syntax near '|'. " |
|
#4
|
|||
|
|||
|
|| isn't an OR operation, it is a string concatenation. But it shouldn't throw an error. Are you sure you have the correct number of parenthesis around the functions?
|
|
#5
|
|||
|
|||
|
Well I completely erased everything and started over. It now works but I would love it if there was a simpler way to state it. There must be a shorter version of what I've written?
Here is the final code which works: Code:
<cfquery name="q_queryResults" datasource="PLOG">
SELECT *
FROM data_admin.Main_log
WHERE del_job = 0
AND 0=0
<CFIF #FORM.Job_Number# IS NOT "">
AND Job_Number LIKE ('%#FORM.Job_Number#%')
</CFIF>
<CFIF #FORM.querystring1# IS NOT "">
AND Refuge_Literal LIKE ('%#trim(FORM.querystring1)#%') OR Requestor LIKE ('%#trim(FORM.querystring1)#%') OR Requestor_Office LIKE ('%#trim(FORM.querystring1)#%') OR Project_Type LIKE ('%#trim(FORM.querystring1)#%') OR Map_Type LIKE ('%#trim(FORM.querystring1)#%') OR Rq_Description LIKE ('%#trim(FORM.querystring1)#%') OR Assigned_To LIKE ('%#trim(FORM.querystring1)#%')
</CFIF>
<CFIF #FORM.querystring2# IS NOT "">
#FORM.querytype# Refuge_Literal LIKE ('%#trim(FORM.querystring2)#%') OR Requestor LIKE ('%#trim(FORM.querystring2)#%') OR Requestor_Office LIKE ('%#trim(FORM.querystring2)#%') OR Project_Type LIKE ('%#trim(FORM.querystring2)#%') OR Map_Type LIKE ('%#trim(FORM.querystring2)#%') OR Rq_Description LIKE ('%#trim(FORM.querystring2)#%') OR Assigned_To LIKE ('%#trim(FORM.querystring2)#%')
</CFIF>
ORDER BY Date_Assigned DESC, Job_Number DESC
</cfquery>
thanks, melissa |
|
#6
|
|||
|
|||
|
Can't test this code, but this is a starting point. It is also fairly complex, but unlike your original code, this can easily have additional form fields or database fields added and handled. Basically, SQL is terrible at doing anything beyond basic searching. Most databases now support free text searching, and CF comes with Verity which handles searching a database much more thoroughly.
Code:
<cfquery name="q_queryResults" datasource="PLOG">
SELECT *
FROM data_admin.Main_log
WHERE del_job = 0
AND 0=0
<CFIF form.Job_Number IS NOT "">
AND Job_Number LIKE ('%#FORM.Job_Number#%')
</CFIF>
<cfset formSearchFieldList = "queryString1,queryString2" >
<cfset targetFieldList = "Refuge_Literal,Requestor_Office,Project_Type,Map_Type,Rq_Description,Assigned_To">
<cfloop index="thisFormField" list="#formSearchFieldList#" delimiters=",">
<cfif len( trim( form[thisFormField] ) )>
<cfloop index="thisDBField" list="#targetFieldList#" delimiters=",">
<cfif listFind( targetFieldList, thisDBField, "," ) eq 1>
AND
<cfelse>
OR
</cfif>
#thisDBField# LIKE ('%#trim( form[thisFormField] )#%')
</cfloop>
</cfif>
</cfloop>
ORDER BY Date_Assigned DESC, Job_Number DESC
</cfquery>
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > ColdFusion Development > search with wildcard - newbie question |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|