Question

The database table contains the fiels as invoice time as type datetime, i have to fetch the data from a given date to current date. I have created the procedure as follows but it doesnt work.. I have the specified start date as 01-04-2014 but the date before this date is also fetched into the table. I have added the condition for selecting the data between specified date but it doesnt work. Someone help me out of this problem.

CREATE DEFINER=`root`@`localhost` PROCEDURE `list_invoice`(invoice_number varchar(30),branch_code varchar(15))

begin    

select `invoice number`,date_format(`invoice date`,'%d-%m-%Y') `invoice date`,`customer name`,sum(b.amount) `total amount`,sum(round(b.amount * a.`tax percentage`,2)) 
`tax amount` from invoices a
inner join `challan master` b on b.`challan number` = a.`challan number`
inner join `customer master` c on c.`customer id` = b.`customer id`
where (((((locate(invoice_number,`invoice number`) > 0 or locate(invoice_number,`customer name`) > 0) and invoice_number <> '')
               or
      (invoice_number = ''))and(`invoice number` like concat('%',branch_code,'%'))) AND (date_format(`invoice date`,'%d-%m-%Y') BETWEEN '01-04-2014' and date_format(CURDATE(),'%d-%m-%Y')))
group by `invoice number`,`customer name`
order by date_format(`invoice date`,'%Y-%m-%d')desc, 1 desc;

end
Was it helpful?

Solution

Add this to the query in where clause:

where ((((locate(invoice_number,`invoice number`) > 0 or locate(invoice_number,`customer name`) > 0) and invoice_number <> '')
               or
      (invoice_number = ''))and(`invoice number` like concat('%',branch_code,'%')) and `invoice date` between date_format(str_to_date('01-04-2014','%d-%m-%Y'),'%Y-%m-%d') and date_format(CURDATE(),'%Y-%m-%d'))
group by `invoice number`,`customer name`
order by date_format(`invoice date`,'%Y-%m-%d')desc, 1 desc;

OTHER TIPS

append to where clause. also check why there is space in 'invoice date'

    AND (date_format(invoice_date,'%d-%m-%Y')>='01-04-2014'
and date_format(invoice_date,'%d-%m-%Y')<=date_format(NOW(),'%d-%m-%Y'))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top