从没有时区的时间戳获取日期

时间:2015-10-21 09:44:07

标签: postgresql psql

下面的查询返回一个表,其中包含没有时区的数据类型时间戳。如何格式化此查询以删除时间戳并保存日期?

SELECT * 
FROM dblink('dbname=mydb', 'select startdate from study') as t1(start timestamp without time zone);

输出:

|-----------start-------|   
|  2012-03-19 00:00:00  |  
|  2012-04-26 00:00:00  |  
|  2012-05-01 00:00:00  |  
|  2012-06-01 00:00:00  |  
|  2012-07-01 00:00:00  |  
|  2012-08-01 00:00:00  |  
|  2012-02-07 00:00:00  |  
|  2012-04-26 00:00:00  |  
|  2012-09-01 00:00:00  |  
|  2012-12-01 00:00:00  |  
|  2013-11-11 00:00:00  |  
+-----------------------+ 

我在寻找:

|------start---|
|  2012-03-19  |  
|  2012-04-26  |  
|  2012-05-01  |  
|  2012-06-01  |  
|  2012-07-01  |  
|  2012-08-01  |  
|  2012-02-07  |  
|  2012-04-26  |  
|  2012-09-01  |  
|  2012-12-01  |  
|  2013-11-11  |  

1 个答案:

答案 0 :(得分:0)

您需要将时间戳字段投射到日期,以下方式可以应用

SELECT * 
FROM dblink('dbname=bhkpps', 'select begins::date from study') as t1(start date);

SELECT * 
FROM dblink('dbname=bhkpps', 'select begins from study') as t1(start date);

SELECT start::date
FROM dblink('dbname=mydb', 'select begins from study') as t1(start timestamp without time zone);