postgresql 从日期只获取月份和年份

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32863802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:02:51  来源:igfitidea点击:

get only month and year from date

sqlpostgresql

提问by khadi8

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

回答by Gordon Linoff

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') ;

回答by Nick Pfitzner

Month:

月:

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

Year:

年:

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

回答by Ivin Raj

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

见页面