Extract Month From Date Field

I have a date field in a postgresql database (field name is input) how can I extract the month only from the date field? I used the syntax below, but I want it to show the actual month name, not a numeric value for the month

EXTRACT(MONTH FROM input) AS "Month"

So if the date in the field input was 04/26/2016 this syntax returns 4, how could I alter this to return April

Upvotes: 12

Views: 34857

Answers (3)

mxn
mxn

Reputation: 1

select date_trunc('month', input)::date as Monat

Ref https://www.postgresql.org/docs/current/functions-datetime.html date_trunc ( text, timestamp ) → timestamp

Truncate to specified precision; see Section 9.9.2

date_trunc('hour', timestamp '2001-02-16 20:38:40') → 2001-02-16 20:00:00

Upvotes: -1

Ram
Ram

Reputation: 1803

A simpler version.

to_char(input, 'Month') AS Month

Source : Data Type Formatting Functions

Upvotes: 23

user1123335
user1123335

Reputation:

You may find this useful

SELECT to_char(to_timestamp (date_part('month', timestamp '2016-04-26 20:38:40')::text, 'MM'), 'Month')

All the best

Ref Postgresql (Current) Section 9.9. Date/Time Functions and Operators

Upvotes: 10

Related Questions