简体   繁体   中英

How to fix SQL query for date function?

I have written the following SQL function for Monthly status report. But I am getting the reports from 30 September to 30 October. I want the results from 1 October to 31 October. What changes must be done?

I have used following datediff function.

SELECT DISTINCT
    d.reports_to_name AS “Manager Name:Data:190”,
    d.employee_name AS “Employee Name:Data:150”,
    d.date AS “DSR Date:Date:120”,
    (CASE 
        WHEN d.holiday = 1 THEN ‘Holiday’ 
        WHEN d.dsr_pause = 1 THEN ‘On Leave’ 
        WHEN d.dsr_created_on IS NULL THEN ‘Missed DSR’ 
        WHEN d.date = d.dsr_created_on THEN ‘On Time’ 
        ELSE ‘Delayed’ 
     END) AS ‘DSR Details:Data:120’,
    DATEDIFF(d.dsr_created_on,d.date) AS “Delayed By:Data:100”,
    d.todays_tasks AS ‘Tasks List:Text:500’
FROM
    tabCalendar DSR AS d
WHERE
    DATEDIFF(NOW(), d.date) BETWEEN 1 AND 31
ORDER BY 
    d.reports_to_name, d.employee_name, d.date ASC

I want the results to be from 1st October to 31st October or from 1st to 31st/ 30th of any month.

Just change the where clause, this will return data from 1st date to last date of current month.

WHERE d.date 
BETWEEN DATE_SUB(LAST_DAY(NOW()),INTERVAL DAY(LAST_DAY(NOW()))-1 DAY) 
AND LAST_DAY(NOW())

If you have date format like 'dd/mm/yyyy' you can use the below query

WHERE d.date
DATE_FORMAT(DATE_SUB(LAST_DAY(NOW()),INTERVAL DAY(LAST_DAY(NOW()))-1 DAY),"%d/%l/%Y")
AND DATE_FORMAT(LAST_DAY(NOW()), "%d/%l/%Y")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM