records from yesterday in postrgesql

Postgresql

Postgresql Problem Overview


I have an application in which I've used MySQL. I had a report that stretched records of the last 24 hours. I used the query:

WHERE (DATE_SUB(CURDATE(), INTERVAL 1 DAY) <= FROM_UNIXTIME(`workorder`.`CREATEDTIME` / 1000))

Now I have to use PostgreSQL and do not know how to make a report of the last 24 hours. Can any of you help?

Postgresql Solutions


Solution 1 - Postgresql

WHERE workorder.createdtime > current_date - 1     -- Yesterday and today

WHERE workorder.createdtime > current_timestamp - interval '1 day' -- last 24hr

Solution 2 - Postgresql

> TIMESTAMP 'yesterday'

For convenience, Postgres includes a few hard-coded values as special Date/Time inputs. They include:

  • yesterday
  • today
  • tomorrow
  • now

Try SELECT TIMESTAMP 'now'.

For example, here is a query.

SELECT when_row_created_
FROM customer_
WHERE when_row_created_ > TIMESTAMP 'yesterday' 
ORDER BY when_row_created_ DESC
;

These commands may not be appropriate to production code, but they certainly are handy in development. Read the docs and do some practice to be sure you understand the behavior of these commands, how the session’s time zone affects them and so on.

Downsides include (a) implicitly ignoring the crucial issue of time zone, and (b) not standard SQL.

Solution 3 - Postgresql

where workorder.createdtime >= now() - interval '24 hour' 

Solution 4 - Postgresql

WHERE workorder.createdtime::date = current_date - 1; --from yesterday

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKrokodyleView Question on Stackoverflow
Solution 1 - PostgresqlCurtView Answer on Stackoverflow
Solution 2 - PostgresqlBasil BourqueView Answer on Stackoverflow
Solution 3 - PostgresqlRoman PekarView Answer on Stackoverflow
Solution 4 - PostgresqlLucky RatnawatView Answer on Stackoverflow