0

I have the following query which returns the date as dd/mm/yyyy hh:mm:ss, but I would like to extract the time and date as separate columns, how can I do this please? I am new to PostgreSQL

Thanks Kath

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

2 Answers 2

1

use explicit casting, for time:

es.created::time

and for date:

es.created::date

Eg:

t=# select now()::time(0) "Time",now()::date "Date";
   Time   |    Date
----------+------------
 08:19:59 | 2017-06-06
(1 row)
Sign up to request clarification or add additional context in comments.

3 Comments

This has worked to a point, the date and time are now separate (This is great), however the format of the date is still dd/mm/yyyy hh:mm:ss eg 06/06/2017 00:00:00
this is totally different question - ask it. but please first look for the existing answer - I'm sure it is answered on SO already
@KathH that's just how your client displays it. PostgreSQL sends a date only for es.created::date (which does not have any time component in it).
0

Use to_char. See 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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.