I need a SQL statement to delete row that are older than 30 days.
My table events
has a field date
that contains the date and the time it was inserted in the database.
Will this work?SELECT * from Results WHERE [Date] >= DATEADD(d, -30, getdate())
So the delete statement is used to delete rows from a table using the where clause to select only the rows to be deleted. Always use a unique identifier to locate the rows that you need to delete. To delete all the rows in a table, always use TRUNCATE TABLE.
SELECT * FROM product WHERE pdate >= DATEADD(day, -30, getdate()).
It is used in the DELETE LIMIT statement so that you can order the results and target those records that you wish to delete. It specifies a limited number of rows in the result set to delete based on row_count. For example, LIMIT 10 would delete the first 10 rows matching the delete criteria.
Another way to delete multiple rows is to use the IN operator. DELETE FROM table_name WHERE column_name IN (value 1, value 2, value 3, etc...); If you want to delete all records from the table then you can use this syntax.
Use DATEADD in your WHERE clause:
... WHERE date < DATEADD(day, -30, GETDATE())
You can also use abbreviation d
or dd
instead of day
.
You could also use
SELECT * from Results WHERE date < NOW() - INTERVAL 30 DAY;
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