Insertion of dates in Postgresql

Asked

Viewed 244 times

1

I would like to know how to configure the Postgre to accept date entries as follows: "12122016".

I know that with the same "spelling" is possible with YMD more I would like to know how to set up to accept so.

Thanks for your attention.

  • If it stays 12/12/2016 solve? The date will depend on the locale you set in the database, but then pt_br will look like this / . I do not know if it has to customize from scratch, when I need to format usually do this on the application side.

  • Sorry it took so long to answer, the ideal would be without the separator characters, because I am reading a file of many lines and generating queries of Insert from it so to give a treatment of a specific type is impossible because they are many Inserts of many different types. Still appreciate the hunch :)

1 answer

1

Willian,

postgres has the parameter datestyle which allows you to configure the default date format of your cluster (if configured in postgresql.conf) or your database (ALTER DATABASE database_name SET datestyle TO "ISO, DMY";), however I did not find anything in this format ddMMyyyy, as your example. The default for this parameter is: ISO, DMY

However, when performing INSERT you can use the function to_date to convert that information.

insert into nome_tabela (campo_data) values (to_date('12122016','ddMMyyyy'));

https://www.postgresql.org/docs/current/static/runtime-config-client.html#GUC-DATESTYLE https://www.postgresql.org/docs/current/static/functions-formatting.html

  • Sorry it took me so long to reply, as I said above I’m generating queries of Inserts from a file of many lines so the way above would really work though, treating a specific field where you are creating queries of many different entities is impossible. Even so thank you for answering if it were not for this my complicated scenario rsrs would be sure

Browser other questions tagged

You are not signed in. Login or sign up in order to post.