2017-06-06 84 views
0

我有以下查询返回日期为dd/mm/yyyy hh:mm:ss,但我想提取时间和日期作为单独的列,我该如何做到这一点?我是新来的Pos​​tgreSQLPostgreSQL获取日期

感谢 凯丝

select 
    up.mis_id, 
    up.name, 
    up.surname, 
    es.created as "signed in", 
    es1.created as "signed out", 
    bb.name as "location" 


from users_person as up 

join users_role as ur on ur.id = up.role_id 
join events_event as ee on ee.user_id = up.id 
join events_swipe as es on es.id = ee.sign_in_id 
join events_swipe as es1 on es1.id = ee.sign_out_id 
join buildings_building as bb on bb.id = es.location_id 

回答

1

使用显式类型转换,时间:

es.created::time 

和日期:

es.created::date 

如:

t=# select now()::time(0) "Time",now()::date "Date"; 
    Time | Date 
----------+------------ 
08:19:59 | 2017-06-06 
(1 row) 
+0

这工作到一个点,日期和时间现在是独立的(这是伟大的),日期但格式仍然是DD/MM/YYYY HH:mm:ss的如06/06/2017 00:00:00 – KathH

+0

这是完全不同的问题 - 问问。但请首先寻找现有的答案 - 我相信它已经回答了SO已经 –

+0

@KathH这就是您的客户端如何显示它。 PostgreSQL只为'es.created :: date'(它没有任何时间组件)发送'date'。 – pozs

0

使用to_char。见Postgres的documentation

select to_char(current_timestamp, 'HH12:MI:SS') as time, 
     to_char(current_timestamp, 'mm/dd/yyyy') as date; 
     time | date  
    ----------+------------ 
    04:43:13 | 06/06/2017 
    (1 row) 
+0

谢谢你的工作非常出色! – KathH