In PostgreSQL puoi usare EXTRACT() funzione per ottenere il mese da una data.
Puoi anche usare il DATE_PART() funzione per fare la stessa cosa.
Esempio 1:La funzione EXTRACT()
Ecco un esempio di utilizzo di EXTRACT() funzione per estrarre il mese da una data.
SELECT EXTRACT(
MONTH FROM TIMESTAMP '2020-12-16 10:41:35'
) AS "Month";
Risultato:
Month
-------
12
Ecco un altro esempio che utilizza il timestamp corrente.
SELECT
current_timestamp,
EXTRACT(
MONTH FROM current_timestamp
) AS "Month";
Risultato:
current_timestamp | Month ------------------------------+------- 2020-03-05 09:15:19.89834+10 | 3
Esempio 2:La funzione DATE_PART()
Ecco un esempio di utilizzo di DATE_PART() funzione invece.
SELECT
current_timestamp,
DATE_PART(
'month', current_timestamp
) AS "Month";
Risultato:
current_timestamp | Month -------------------------------+------- 2020-03-05 09:16:53.587544+10 | 3