Skip to main content
Inspiring
Mar 17, 2021 3:30 PM UTC
Answered

DATEDIFF / MySQL Question

I have a query where I am trying to get all records from a data table (TIMELOG) that have happened (EVENTTIME) in the past 7 days or less.

 

The table name is TIMELOG and the field name is EVENTTIME.

I want all records where EVENTTIME is in the past 7 days or less.

SELECT * FROM TIMELOG
WHERE
#DateDiff("d",EVENTTIME,now())# < 7

I don't know why this doesn't work.
I get this error message
Variable EVENTTIME is undefined. 

    This topic is closed to new replies. Start a new post to keep the conversation going.
    BKBK
    BKBKCorrect answer
    Community Expert
    Mar 17, 2021 5:55 PM UTC

    You were getting the error because EVENTTIME, being a database column, is unknown to ColdFusion. A simple solution is to use MSQL date functions:

     

    SELECT *
    FROM TIMELOG
    WHERE DATEDIFF(CURDATE(),EVENTTIME) < = 7

    3 replies

    weezerboyAuthor
    Inspiring
    Mar 17, 2021 5:50 PM UTC

    I figured it out. Thank you all

    Translate
    BKBK
    Community Expert
    BKBKCommunity ExpertCorrect answer
    Community Expert
    Mar 17, 2021 5:55 PM UTC

    You were getting the error because EVENTTIME, being a database column, is unknown to ColdFusion. A simple solution is to use MSQL date functions:

     

    SELECT *
    FROM TIMELOG
    WHERE DATEDIFF(CURDATE(),EVENTTIME) < = 7
    Translate
    weezerboyAuthor
    Inspiring
    Mar 17, 2021 7:41 PM UTC

    This is exactly what I did BKBK. Thank you

    Translate