Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select a date that is 4 days ago at this time?

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 */;
like image 446
Adam Avatar asked Jun 06 '12 20:06

Adam


People also ask

How do I subtract one day from a date in SQL?

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).

Can we use datediff in where clause?

Note: DATEADD and DATEDIFF SQL function can be used in the SELECT, WHERE, HAVING, GROUP BY and ORDER BY clauses.

How do I check if a date is greater than today in SQL?

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.


1 Answers

believe it or not, it's super-easy.

SELECT * FROM mytable WHERE created_at > sysdate - 4
like image 183
user158017 Avatar answered Nov 14 '22 21:11

user158017