Skip to content

Instantly share code, notes, and snippets.

@dsaiztc
Last active May 30, 2018 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsaiztc/d041b191c40d74a5d1cf9e6db86a4c54 to your computer and use it in GitHub Desktop.
Save dsaiztc/d041b191c40d74a5d1cf9e6db86a4c54 to your computer and use it in GitHub Desktop.
/* https://stackoverflow.com/a/30783772/3149679 */
WHERE
created >= date_trunc('week', CURRENT_TIMESTAMP - interval '1 week')
AND
created < date_trunc('week', CURRENT_TIMESTAMP)

Select part of a timestamp:

SELECT date_part('day', TIMESTAMP '2001-02-16 20:38:40');
Result: 16

SELECT date_part('hour', INTERVAL '4 hours 3 minutes');
Result: 4

Trunc part of the timestamp:

SELECT date_trunc('hour', TIMESTAMP '2001-02-16 20:38:40');
Result: 2001-02-16 20:00:00

SELECT date_trunc('year', TIMESTAMP '2001-02-16 20:38:40');
Result: 2001-01-01 00:00:00
-- Get total seconds in '1 day 15:11:10.123'
SELECT EXTRACT(EPOCH FROM '1 day 15:11:10.123'::INTERVAL)
# 141070.123
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment