Selecting part of a date

You want to compare/calculate/display part of a date/time (an hour, day, month etc).

Solution

Use the date_part(<part>,<date | time | interval>) function ref.

Discussion

Sometimes you want to summarise results per month or select entries for the first day of the month. The simplest way to do this is to use the date_part() function. So, to see all orders placed in January, use:

SELECT * FROM orders where date_part('month',ord_placed)=1;
   

Note that this gives orders from January 2000 and January 2001 - make sure to restrict the year as well if that is what you want:

SELECT * FROM orders where date_part('month',ord_placed)=1 AND date_part('year',ord_placed)=2001;
   

You can use date_part to return `year', `month', `day', `hour', `minute', and `second' and 'dow' for the day of the week (Sunday is 0, Saturday is 6).