1
我想運行一個函數來返回兩個日期之間的天差。出於這個原因,我根據日期參數獲取兩個日期(這工作正常)。接下來,我使用參數日期pdate
來構造日期,如果它們未在表中找到。日期的這種構造不能按需要工作。以下是代碼:MYSQL STR_TO_DATE不返回想要的結果
`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<=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 ;`
我得到的錯誤如下: Incorrect datetime value: '2013-8-21' for function str_to_date
這是在爲v_FromDate
轉換。