2017-06-06 121 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