2

I've a createdAt field in a PostgreSQL table which is of the type timestamp with time zone. I want to change the format of it to include the milliseconds.

How do you suggest to change the timestamp format using the SQL query from 2018-08-21 10:35:28+00 to 2018-08-21 10:35:28.000+00?

Update:

Basically I want to convert second row createdAt to first row format with milliseconds.

enter image description here

4
  • 1
    Are you sure that the current timestamps do not include the millis? Or does your database viewer is just not showing them? What is the result of SELECT EXTRACT(MILLISECONDS FROM <your timestamp column>); This datatype should store the milliseconds. Commented Oct 5, 2018 at 10:08
  • I'm working with the Sails ORM which has recently changed the format they save for createdAt and I need to make the data consistant. On running the above query, I'm receiving 28000 as date_part which is showing milliseconds as 000 which is fine. I want to add that to the createdAt field. Commented Oct 5, 2018 at 10:18
  • 1
    Have you tried to use the to_char function (ie SELECT to_char(createdAt, 'YYYY-MM-DD HH:MI:SS.MS TZ') ) ? Commented Oct 5, 2018 at 10:34
  • 1
    But if you tried with the query and you got the 000 ms what is your problem? Did you inserted a new timestamp and the ms are truncated? If the old data do not have any ms parts they are stored with 000 ms. How would you like to add some ms parts? It seems that your second row does not have any ms part so your viewer just does not show ".000". But in the timestamp format the part exist internally. I am not sure if I understood your problem correctly. Commented Oct 5, 2018 at 10:53

1 Answer 1

3

If you want your timestamp to be displayed in a specific format, you have to format it using the to_char function:

SELECT to_char(TIMESTAMPTZ '2018-08-21 10:35:28', 'YYYY-MM-DD HH24:MI:SS.MSOF');

          to_char           
----------------------------
 2018-08-21 10:35:28.000+00
(1 row)
Sign up to request clarification or add additional context in comments.

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.