CrudVision – Lisa Seelye

August 6, 2007

Postgres “gotcha”

Filed under: postgres,snippet — Lisa Seelye @ 08:45

In my evedb.info efforts I need a quick way to expire old market orders. The CSV has a duration column that is an integer number of days. Postgres allows its users to typecast between various data types in SQL so there doesn't need to be a SELECT duration FROM ... ; (in code) to_delete = Time.now + duration.days ; DELETE FROM ... WHERE duration < to_delete;

It just so happens that the form for casting an integer to do math with a timestamp is a bit funky. An integer may not be directly added to a timestamp. The magic is:

SQL:
  1. DELETE
  2. FROM foo
  3. WHERE now()> (created_on + CAST( (duration || ' days') AS INTERVAL));

The || ' days' I don't understand just yet but it works!

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress