Note that there are some explanatory texts on larger screens.

plurals
  1. POMYSQL STR_TO_DATE not returning desired results
    primarykey
    data
    text
    <p>I am trying to run a function to return the difference of days between two dates. For this reason, I fetch two dates based on a date parameter (this is working fine). Next, I use the parameter date <code>pdate</code> to construct dates if they are not found in a table. This construction of dates is not working as desired. Following is the code:</p> <pre><code>`DELIMITER $$ USE payrolldb001 $$ DROP FUNCTION IF EXISTS NoOfWorkingDays $$ CREATE DEFINER=`root`@`localhost` FUNCTION NoOfWorkingDays(P_EmployeeID INT,pdate DATE) RETURNS INT(10) READS SQL DATA BEGIN DECLARE v_FromDate DATE; DECLARE todate DATE; DECLARE workingdays INT; DECLARE shiftoff INT; DECLARE gazzetted INT; SET v_FromDate=(SELECT cp.FromDate FROM current_payroll cp INNER JOIN employees e ON e.employeeid=P_EmployeeID AND e.CompanyCode=cp.CompanyCode AND e.SubOfficeCode=cp.SubOfficeCode WHERE pdate BETWEEN cp.FromDate AND cp.ToDate); IF (v_FromDate IS NOT NULL) THEN SET todate=(SELECT cp.ToDate FROM current_payroll cp INNER JOIN employees e ON e.employeeid=P_EmployeeID AND e.CompanyCode=cp.CompanyCode AND e.SubOfficeCode=cp.SubOfficeCode WHERE pdate BETWEEN cp.FromDate AND cp.ToDate); ELSE SET v_FromDate=STR_TO_DATE(CONCAT(YEAR(pdate),'',MONTH(pdate)-1,'-21'),"%d%m%Y"); SET todate=STR_TO_DATE(CONCAT(YEAR(pdate),'-',MONTH(pdate),'-20'),"%d%m%Y"); END IF; SET workingdays = DATEDIFF(todate,v_FromDate); WHILE v_FromDate&lt;=todate DO SET shiftoff= `isShiftOff`(P_EmployeeID,v_FromDate); IF (shiftoff=0) THEN SET gazzetted = `GazettedHoliday`(v_FromDate,P_Employeeid); SET workingdays = workingdays-gazzetted; ELSE SET workingdays=workingdays-shiftoff; END IF; -- RESET QUERY CACHE; -- FLUSH QUERY CACHE; SET v_FromDate = ADDDATE(v_FromDate,1); END WHILE; RETURN workingdays ; END$$ DELIMITER ;` </code></pre> <p>The error I am getting is as follows: <code>Incorrect datetime value: '2013-8-21' for function str_to_date</code> which is in the conversion for <code>v_FromDate</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload