简体   繁体   中英

How to get Data of last 7 days

I am trying to get data of the last 7 days but my code is not working. Getting "DATEADD function does not exist".

I have a date in this format: 2019-03-05T10:59:09.2996542+00:00

SELECT *
FROM finaltest1
WHERE CAST(Date AS DATE) > DATEADD(DAY, -7, CAST(GETDATE() AS DATE)) ;

DATEADD is SQL, the MySQL equivalent is DATE_ADD (or to subtract, DATE_SUB ). The parameters are different too, the first is the date value and the second the interval of time you want to add. Also note that in MySQL, the GETDATE equivalent is CURDATE() . For your query, this should work, assuming your Date column is in a valid format:

SELECT *
    FROM finaltest1
    WHERE CAST(Date AS DATE) > DATE_SUB(CURDATE(), INTERVAL 7 DAY)

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