Click to See Complete Forum and Search --> : search query problem
raj_2006
10-08-2006, 04:27 PM
Hi
I have a date type field st.
I have a record in the db where st=2006-10-06
So cant i fetch this record if i use this query:
$sql="SELECT * FROM transaction WHERE st BETWEEN STR_TO_DATE('$start', '%Y/%m/%d') AND STR_TO_DATE('$end', '%Y/%m/%d')";
I am sending
$start=2006-10-06
$end=2006-10-06
But unfortunately no rows is fetched.
Plz suggest me if i am doing any wrong with the sql.
Thanks
Raj
chazzy
10-08-2006, 08:01 PM
what's the type on st?
raj_2006
10-09-2006, 01:00 AM
The data type of st is date
0000-00-00
//i am inserting as $st=date("Y-m-d");
chazzy
10-09-2006, 07:59 AM
are you positive it is a date? typically, you can't insert in that format if it is a date. can you run a describe on the table and show the output? the issue you're describing is when the column is not a date.
another thing you can try is
SELECT * FROM transaction WHERE ((st BETWEEN STR_TO_DATE('$start', '%Y/%m/%d') AND STR_TO_DATE('$end', '%Y/%m/%d')) OR (st = STR_TO_DATE('$start', '%Y/%m/%d')) OR (st = STR_TO_DATE('$end', '%Y/%m/%d')) ;
raj_2006
10-09-2006, 09:53 AM
Field Type Null Key Default Extra
tid int(9) PRI NULL auto_increment
st date 0000-00-00
This is the table desc.
and also while running this query i am getting sql error
SELECT * FROM transaction WHERE ((st BETWEEN STR_TO_DATE('2006-10-6', '%Y/%m/%d') AND STR_TO_DATE('2006-10-6', '%Y/%m/%d')) OR (st = STR_TO_DATE('2006-10-6', '%Y/%m/%d')) OR (st = STR_TO_DATE('2006-10-6', '%Y/%m/%d')) ;
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1.
chazzy
10-09-2006, 09:56 AM
depending on how you execute this, you probably don't want the ' ;' included in your query.
raj_2006
10-09-2006, 10:11 AM
sorry i am using in this way:
$sql="SELECT * FROM transaction WHERE ((st BETWEEN STR_TO_DATE('$start', '%Y/%m/%d') AND STR_TO_DATE('$end', '%Y/%m/%d')) OR (st = STR_TO_DATE('$start', '%Y/%m/%d')) OR (st = STR_TO_DATE('$end', '%Y/%m/%d'))" ;
chazzy
10-09-2006, 10:41 AM
based on your input, shouldn't it be %Y-%m-%d?
raj_2006
10-09-2006, 10:51 AM
I tried with given - in place of /
but still its giving sql error.....
$sql="SELECT * FROM transaction WHERE ((st BETWEEN STR_TO_DATE('$start', '%Y-%m-%d') AND STR_TO_DATE('$end', '%Y-%m-%d')) OR (st = STR_TO_DATE('$start', '%Y-%m-%d')) OR (st = STR_TO_DATE('$end', '%Y-%m-%d'))" ;