如何从postgres的时间戳列中获取日或年数

6 sql postgresql

我在 postgres 上这样的表中有一个时间戳列

CreatedAt

2016-10-26 07:33:51.029
2016-11-01 08:39:12.322
Run Code Online (Sandbox Code Playgroud)

我需要得到像

SELECT day number or may be month number 
FROM "MyTable";
Run Code Online (Sandbox Code Playgroud)

预期输出将是

day

26
01
Run Code Online (Sandbox Code Playgroud)

a_h*_*ame 6

使用extract()函数:

select extract(day from createdat) as day,
       extract(year from createdat) as year
from "MyTable";
Run Code Online (Sandbox Code Playgroud)

手册中的更多详细信息:https : //www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT


dea*_*ool 1

你可以试试

SELECT to_char("your column",'DD') as "day" 
FROM "MyTable";
Run Code Online (Sandbox Code Playgroud)

更多信息请访问https://www.postgresql.org/docs/current/static/functions-formatting.html