Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MySQL Date range or IS NULL

Status
Not open for further replies.

deeciple

Technical User
Mar 1, 2012
70
US
Hi All,

I have a form that populates search criteria in a query query. I want the user to be able to select a date range if they want to but also be able to use other search criteria and not use the range. My query seems to only be working if the date range fields are filled. Otherwise no results are being returned. I have used IS NULL before but that was in MS Access. It doesn't seem to be working here but I am probably not using it correctly. How would I do this in MySQL? Here is the query:

SQL:
	$query ="SELECT *
			 FROM 	tblequipissues
			 WHERE 	Location LIKE '%$Location%' AND
					LocationNum LIKE '%$LocationNum%' AND
					EquipType LIKE '%$EquipType%' AND
					UnitNo LIKE '%$EquipNum%' AND
					IssueDate BETWEEN '$FromIssueDate' AND '$ToIssueDate' AND
					EnteredBy LIKE '%$EnteredBy%' AND
					FixedBy LIKE '%$Assigned%'";

Thanks!

Ken
 
but also be able to use other search criteria and not use the range.

php is a pretty powerful language

write an IF test in php so that if no date range is specified, then remove those lines from the sql statement

easy peasy

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Thanks for your reply r937. This worked for me:

SQL:
(IssueDate >= '$FromIssueDate' OR '$FromIssueDate' = '') AND 
      				(IssueDate <= '$ToIssueDate' OR '$ToIssueDate' = '')

Switching from NULL to an empty string did the trick. I also went from the BETWEEN evaluation to two separate tests (one for >= and the other for <=) for more flexibility.

Kind regards,

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top