Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 仅获取从日期开始的月份和年份_Sql_Postgresql - Fatal编程技术网

Sql 仅获取从日期开始的月份和年份

Sql 仅获取从日期开始的月份和年份,sql,postgresql,Sql,Postgresql,我只想从日期开始得到年份和数字月份,我试着通过我得到字符月份来计算月份,我怎样才能得到数字月份和年份 select to_char(dateper,'MON') from calcul group by dateper 您可以使用以下方法获取字符串中的数字表示: select extract(year from dateper) as yyyy, extract(month from dateper) as mm from calcul group by yyyy, mm; 或: 你可以

我只想从日期开始得到年份和数字月份,我试着通过我得到字符月份来计算月份,我怎样才能得到数字月份和年份

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

您可以使用以下方法获取字符串中的数字表示:

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

你可以试试这个:

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

请参阅本页

月份:

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


它返回的年份函数不存在exist@khadi8 . . . 哎呀。我忘了我在本地版本的Postgres中定义了这些函数。这些是我的职责,不是博士后的。正确的函数是
extract()
,这是ANSI的标准函数。一个有用的参考-stackoverflow.com/a/29839100/3682599和一个示例-这对Postgres无效:字符串连接是使用SQL中的
|
来完成的,Postgres中既没有
datepart()
也没有
datename()
select to_char(DATEFIELD,'MON') from YOUR_TABLE
SELECT date_part('month', now())::integer;
SELECT date_part('year', now())::integer;