khadi8
khadi8

Reputation: 65

get only month and year from date

I want get only year, and only numeric month from date, I try this for month by i get char month, how can i get numeric month, and year ?

select to_char(dateper,'MON') 
from calcul
 group by dateper

Upvotes: 0

Views: 23162

Answers (3)

Gordon Linoff
Gordon Linoff

Reputation: 1269593

You can get a numeric representation in a string by using:

select extract(year from dateper) as yyyy, extract(month from dateper) as mm
from calcul
group by yyyy, mm;

Or:

select to_char(dateper, 'YYYY-MM') 
from calcul
group by to_char(dateper, 'YYYY-MM') ;

Upvotes: 1

Nick Pfitzner
Nick Pfitzner

Reputation: 206

Month:

SELECT date_part('month', now())::integer;

Year:

SELECT date_part('year', now())::integer;

Upvotes: 1

Ivin Raj
Ivin Raj

Reputation: 3429

You can try this one:

datename(m,column)+' '+cast(datepart(yyyy,column) as varchar) as MonthYear

or

select to_char(DATEFIELD,'MON') from YOUR_TABLE

please Refer This Page

See The Page

Upvotes: 0

Related Questions