0

I'm working with Postgres TIMESTAMP data types and want to change the display format anytime a SELECT query is called.

I currently have it working as shown below:

SELECT to_char(my_time_col, 'YYYY/MM/DD HH24:MI:SS') as time_col FROM my_table

Is there a way to have it formatted as 2018/05/31 13:00:00 for all queries either via some predefined function, or trigger (or something that I don't know about) without having to manually format the DATETIME fields for each individual query? In other words — I just want to type this:

SELECT my_time_col FROM my_table

Thanks in advance.

3
  • Have you tried to look for it? There's plenty in the official documentation: postgresql.org/docs/9.6/static/functions-formatting.html Commented Feb 3, 2018 at 3:08
  • Your SQL client formats the timestamp value. Check the configuration of the SQL client you are usnig Commented Feb 3, 2018 at 6:31
  • that depends if the client is using the binary or the ascii interface. Commented Feb 3, 2018 at 11:15

2 Answers 2

1

run this sql:

SET datestyle to 'ISO, DMY';

so long as you don't store fractional seconds you should get the result you want.

this setting is temporary but can be made permanent by prefixing it with

ALTER USERusername

or

ALTER DATABASEdatabasename

Sign up to request clarification or add additional context in comments.

Comments

0

There is no such setting.

The best thing is to let the application take care of the formatting.

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.