INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

MySQL Date range or IS NULL

MySQL Date range or IS NULL

(OP)
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:

CODE --> 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

RE: MySQL Date range or IS NULL

Quote:

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

RE: MySQL Date range or IS NULL

(OP)
Thanks for your reply r937. This worked for me:

CODE --> 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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close