Jump to content

[SOLVED] date range query


gevensen

Recommended Posts

i cant seem to get a query working using  name and date range (between start and end date)

all values are in the tables mysql doesnt return an error

any ideas?

mysql 5

php 5.2.8

 

$sql_query="SELECT recnum FROM sc_income WHERE ( full_name = 'gary' AND ( service_date >= '01/01/2009' AND service_date <= '08/01/2009') ) ";

Link to comment
https://forums.phpfreaks.com/topic/170092-solved-date-range-query/
Share on other sites

You CANNOT do greater-than/less-than date comparisons using that format '01/01/2009' because the fields that make up that format are not ordered left-to-right, MSD (Most Significant Digit - YEAR) to LSD (Least Significant Digit - DAY).

 

You need to use a DATE data type YYYY-MM-DD to get greater-than/less-than date comparisons to work. A DATE data type will also use less storage than the format you are currently using.

01/01/2009 is not a valid date format. If you have service_date as a varchar field then you have made an error. It must be of type DATE (YYYY-MM-DD) to use operators on. Essentially this is a string '01/01/2009' so how can it be greater or less than another value?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.