Home Delete records in PostgreSQL, older than X days, where X is a parameter
Reply: 1

Delete records in PostgreSQL, older than X days, where X is a parameter

Pavitx
1#
Pavitx Published in 2018-02-13 16:33:10Z

I want to delete records older than a given (as a parameter to the function) number of days.

There are similar questions (e.g. SQL Get all records older than 30 days) which recommend to use interval '30 day' but how to make the 30 a parameter?

I have a column with a timestamp and the delete would be done in a function that's in SQL rather than plpgsql

Laurenz Albe
2#
Laurenz Albe Reply to 2018-02-13 17:02:29Z

Such an SQL function could look like this:

CREATE FUNCTION delold(integer) RETURNS void
   LANGUAGE sql STRICT AS
'DELETE FROM mytable
 WHERE tscol < current_timestamp
               - ($1::text || '' days'')::interval';
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.299131 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO