I need to query to get rows where a timestamp
was after exactly 4 days ago this time.
For example if I run the query at 1pm on June 6, 2012 it should select rows where the date is after 1pm on June 2, 2012. How do I do this?
SELECT * FROM mytable WHERE created_at < /* 4 days ago this time */;
The DATEADD() function takes three arguments: datepart , number , and date . Here, the value of datepart is day , because the unit of time you want to subtract is day. The second argument is -1 (you subtract 1 day, which is the same as adding -1 day).
Note: DATEADD and DATEDIFF SQL function can be used in the SELECT, WHERE, HAVING, GROUP BY and ORDER BY clauses.
GETDATE() function: This function is used to return the present date and time of the database system. After comparison column contains the following string: Lesser than- If the date is less than today's date. Greater- If the date is greater than today's date.
believe it or not, it's super-easy.
SELECT * FROM mytable WHERE created_at > sysdate - 4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With