Timestamps are crucial to business analysis for a very simple reason: they tell you when things happen. Imagine trying to suss out trends in your data, like monthly web traffic, or quarterly earnings, or daily order volume without knowing when events occurred. It'd be a nightmare.offers a variety of date functions for manipulating timestamps. To separate the useful from the obscure, we're sharing how-tos for the most frequently used Postgres date functions and business scenarios where they come in handy.We've made the data for each example available in the.
Try each date function in Mode as you work your way through these examples. For an account and to begin. Rounding off timestamps with DATETRUNCThe DATETRUNC function rounds a timestamp value to a specified interval, which allows you to count events.
You can round off a timestamp to the following units of time:. microsecond. millisecond. second. minute.
hour. day.
week. month. quarter.

year. decade. century.
milleniumThe DATETRUNC syntax looks like this: DATETRUNC('interval',timestamp).For example, SELECT DATETRUNC('day','2015-04-12 14:44:18') would return a result of 2015-04-12 00:00:00.For a more detailed explanation of DATETRUNC (and a printable reference you can keep at your desk!), check out. How has web traffic changed over time?Try DATETRUNC for yourself by querying the table, which contains sample records of website visits, including an occurredat column. You can isolate the month of the visit with DATETRUNC. SELECT DATETRUNC('month',occurredat) AS monthFROM demo.webeventsWHERE occurredat BETWEEN '2015-01-01' AND '2015-12-31 23:59:59'To return a count of web visits each month by channel, add the channel column and a COUNT to the SELECT statement, then group by month and channel.
(Since month and channel are the first two values in your SELECT statement, you can GROUP BY 1,2), like this: SELECT DATETRUNC('month',occurredat) AS month,channel,COUNT(id) AS visitsFROM demo.webeventsWHERE occurredat BETWEEN '2015-01-01' AND '2015-12-31 23:59:59'GROUP BY 1,2Finally, use ORDER BY 1,2 to organize your results chronologically (by month) and alphabetically (by channel). SELECT DATETRUNC('month',occurredat) AS month,channel,COUNT(id) AS visitsFROM demo.webeventsWHERE occurredat BETWEEN '2015-01-01' AND '2015-12-31 23:59:59'GROUP BY 1,2ORDER BY 1,2In Mode, you can build a line chart to visualize the query results.Finding events relative to the present time with NOW and CURRENTDATEThe NOW date function returns the current timestamp in UTC (if the time zone is unspecified). You can subtract intervals from NOW to pull events that happened within the last hour, the last day, the last week, etc.Running SELECT NOW at 9:00am UTC on October 11th, 2016 would result in 2016-10-11 09:00:00.The CURRENTDATE function only returns the current date, not the whole timestamp. Running SELECT CURRENTDATE at 9:00am UTC on October 11th, 2016 would return 2016-10-11.
What orders were placed in the last 12 hours?The table contains sample records of all orders, including an occurredat timestamp column in UTC.To find orders placed in the last 12 hours, use a WHERE clause to return only orders that were placed after or exactly at ( =) the current timestamp ( NOW) minus an interval of 12 hours.

DATEPART(field,source)The field is an identifier that determines what field to extract from the source. The values of the field must be in a list of permitted values mentioned below:. century. decade. year.
month. day. hour. minute. second.
microseconds. milliseconds. dow.
doy. epoch. isodow.
isoyear. timezone.
timezonehour. timezoneminuteThe source is a temporal expression that evaluates to,. If the source evaluates to, the function will cast to TIMESTAMP.The DATEPART function returns a value whose type is double precision.

PostgreSQL DATEPART examplesThe following example extracts the century from a time stamp.