I am trying to retrieve results from this query, but it is not fetching any record:
SELECT DATE_FORMAT(BILLDATE,'%M-%Y'), SUM(GROSSAMOUNT), SUM(NETAMOUNT)
FROM BILLDETAILS
WHERE DATE_FORMAT(BILLDATE,'%M-%Y') BETWEEN 'May-2011' AND 'Jul-2011'
GROUP BY MONTH(BILLDATE), YEAR(BILLDATE)
ORDER BY YEAR(BILLDATE) DESC, MONTH(BILLDATE);
I also want to display SUM(GrossAmount) and Sum(NetAmount) as, for example, 1,10,20,356
Your query is doing a string comparison, not a date comparison. Try something like this instead:
There are a variety of ways to specify the dates in the WHERE clause. Have a look at the Date and Time Functions reference for alterntives. Example:
You may find the STR_TO_DATE function useful for converting strings to dates:
So your
WHEREclause could be:To format the value, use the FORMAT() function:
…although formatting is usually best left to the application. If you format it in MySQL, processing it in the application might become a bit tricky.