Click here to Skip to main content
15,922,533 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Need help on this query. If I run it up and to the 'REPRES' Section it comes back perfectly. The problem is I need to select between those 2 dates in the "date_entered" column as well.

I getting my data from 2 tables named "fine" and "charge" if selects only the data that has "REDIR" and "REPRES" in the 'status' column but if's not filtering out the to select only between those 2 dates.

Please help.


SQL
SELECT fine.case, fine.regnum, fine.issuing_authority, fine.date_entered, charge.amount, fine.status
FROM fine INNER JOIN charge ON fine.case=charge.case
WHERE fine.status = 'REDIR' OR fine.status = 'REPRES'
AND fine.date_entered >= DATE_FORMAT(fine.date_entered, '%d-%m-%Y') = "1/1/2010"
AND fine.date_entered <= DATE_FORMAT(fine.date_entered, '%d-%m-%Y') = "31/12/2010";


This is the result; As you can see the dates has no effect on it.

'1179430141159012', 'ZHD107GP', 'ALBERTON', '9/2/2011', '100', 'REDIR'
'BF80287274110', 'YZD473GP', 'BELFAST', '9/2/2011', '750', 'REDIR'
'1167289141134730', 'VPH261GP', 'ALBERTON', '9/2/2011', '100', 'REDIR'
'0240240055398532', 'YZD461GP', 'JHB', '9/2/2011', '750', 'REDIR'
'0240240054323059', 'ZKW597GP', 'JHB', '9/2/2011', '1500', 'REDIR'
'N80049240120', 'ZWB136GP', 'NELSPRUIT', '9/2/2011', '750', 'REDIR'
'MB80029645105', 'XPT612GP', 'MIDDELBURG', '9/2/2011', '100', 'REDIR'
'0240240057503759', 'XXK353GP', 'JHB', '9/2/2011', '750', 'REDIR'
'0240240054295984', 'YZF152GP', 'JHB', '9/2/2011', '750', 'REDIR'
'0240240054291614', 'SHD923GP', 'JHB', '9/2/2011', '1500', 'REDIR'
'0240240054215571', 'JDG518GP', 'JHB', '9/2/2011', '750', 'REDIR'
'0240240055373045', 'SJV048GP', 'JHB', '9/2/2011', '1500', 'REDIR'
'0240240055326163', 'DDJ186GP', 'JHB', '9/2/2011', '750', 'REDIR'
'0240240055404932', 'SJV048GP', 'JHB', '15/3/2011', '4500', 'REDIR'


Thank you. Any help will be appreciated.

:-)
Posted

your query should be as follow

SQL
SELECT fine.case, fine.regnum, fine.issuing_authority, fine.date_entered, charge.amount, fine.status
FROM fine INNER JOIN charge ON fine.case=charge.case
WHERE fine.status in ('REDIR',  'REPRES')
AND fine.date_entered between DATE_FORMAT(fine.date_entered, '%d-%m-%Y') = "1/1/2010"
AND DATE_FORMAT(fine.date_entered, '%d-%m-%Y') = "31/12/2010";


Try this
 
Share this answer
 
Comments
Dirk C De Winnaar 30-Aug-11 15:11pm    
Error Code: 1064. 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 '= "1/1/2010"
AND DATE_FORMAT(fine.date_entered, '%d-%m-%Y') = "31/12/2010"' at line 4

The second "=" sign is not accepted.
MySQL follows "YYYY-MM-DD" format as default for DateTime.
http://dev.mysql.com/doc/refman/5.0/en/datetime.html

Try by modifying your query as below.

SQL
SELECT fine.case, fine.regnum, fine.issuing_authority, fine.date_entered, charge.amount, fine.status
FROM fine INNER JOIN charge ON fine.case=charge.case
WHERE fine.status = 'REDIR' OR fine.status = 'REPRES'
AND (fine.date_entered BETWEEN '2010-1-1' AND '2010-12-31');


Edited -
SQL
SELECT fine.case, fine.regnum, fine.issuing_authority, fine.date_entered, charge.amount, fine.status
FROM fine INNER JOIN charge ON fine.case=charge.case
WHERE (fine.status = 'REDIR' OR fine.status = 'REPRES')
AND (fine.date_entered BETWEEN '1-1-2010' AND '31-12-2010');
 
Share this answer
 
v2
Comments
Dirk C De Winnaar 30-Aug-11 11:31am    
Hi I know the date format of MySql is yyyy-mm-dd the date inside the table is dd/MM/yyyy.
Query still don't work.
RaisKazi 30-Aug-11 11:35am    
Have a look at updated query. Observe the Parenthisis used in where clause.
Dirk C De Winnaar 30-Aug-11 12:31pm    
Hi RaisKazi. It still does not work. It filters all the needed data but all the other dates from 2011 , 2009, 2010 even if I change the month part to order between the month of '5' eg. It show everything not just that month.
RaisKazi 30-Aug-11 12:56pm    
Ok. Can you change '1-1-2010' AND '31-12-2010' to '1/1/2010' AND '31/12/2010'.
Also, I assume "date_entered" column has DateTime Datatype.
Dirk C De Winnaar 30-Aug-11 13:11pm    
Hi I still don't work. date_entered does not have the DT datatype. It's not a DB I designed. It's another one I'm writting an addition to.
Sorry should have mentioned it before.
:-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900