1

For example I have a following table(tbl_trans) like below

transaction_id  transaction_dte
integer         timestamp without time zone
---------------+----------------------------------
45             |  2014-07-17 00:00:00
56             |  2014-07-17 00:00:00
78             |  2014-04-17 00:00:00

so how can I find the tottal no.of transaction in 7th month from tbl_trans ?

so the expected output is

tot_tran  month
--------+-------
2         | July
rimboche
  • 87
  • 1
  • 12

4 Answers4

3
select count(transaction_id) tot_tran
      ,to_char(max(transaction_dte),'Month') month from tbl_trans
where extract (month from transaction_dte)=7

PostgreSQL Extract function explained here

Reference : Date/Time Functions and Operators

Community
  • 1
  • 1
Vivek S.
  • 19,945
  • 7
  • 68
  • 85
0
select count(transaction_id),date_part('month',transaction_dte)
from tbli_trans where date_part('month',transaction_dte)=7
apomene
  • 14,282
  • 9
  • 46
  • 72
-1
EXTRACT(MONTH FROM TIMESTAMP transaction_dte)

OR

date_part('month', timestamp transaction_dte)

You only need to add the word timestamp if your timestamp is saved in a string format

Properly looked up what the difference between the 2 is now:

The extract function is primarily intended for computational processing. For formatting date/time values for display.

The date_part function is modeled on the traditional Ingres equivalent to the SQL-standard function extract.

Jeremy C.
  • 2,405
  • 1
  • 12
  • 28
-2

Use Datepart function.

where datepart(transaction_dte, mm) = 7
Tminer
  • 302
  • 2
  • 4
  • 14