Sergio
Sergio

Reputation: 303

Setting a default date format on PostgreSQL

Good afternoon,

Many of you may already know that it is possible, for instance, in Oracle set the default date format used by formatting functions (ie to_char), just like this:

ALTER SESSION SET NLS_DATE_FORMAT='SYYYY/MM/DD~HH24:Mi:SS'

My question is: is there any equivalence for this on PostgreSQL. I have been searching for an answer without success although I have found some references to the PostgreSQL locale settings (http://www.postgresql.org/docs/7.4/static/charset.html). Anyhow, I have been unable to figure out an answer.

Any suggestions? Thank you in advance and best wishes for this holidays!

Upvotes: 3

Views: 15243

Answers (2)

Frank Heikens
Frank Heikens

Reputation: 127096

to_char doesn't have a default format, but you could use a variable for this to overcome this issue.

Just create your own to_char function, using a default setting:

CREATE OR REPLACE FUNCTION to_char(timestamptz) RETURNS text AS
$$
    SELECT to_char($1,'YYYY/MM/DD~HH24:Mi:SS'); -- here your default setting
$$
LANGUAGE SQL;

The "normal" to_char function will also work, you still have that option.

Upvotes: 5

user594138
user594138

Reputation:

It's possibly that SET DATESTYLE does what you want, at least for day, month, year portions of the date.

Search here for DateStyle.

Upvotes: 9

Related Questions