pgSQL : insert date format (leading zeros)?

11,405

Solution 1

INSERT INTO TheTable (the_date) VALUES ('yyyy-mm-dd')

is the correct order in SQL

$psql_str = date('yyyy-mm-dd', date_parse_from_format('d-m-yyyy', $date_str));

converts your $date_str to the expcted format.

Solution 2

Check to_date(text, text) function (Table 9-21 contains all supported patterns):

SELECT to_date('1-9-2011', 'DD-MM-YYYY');
  to_date   
------------
 2011-09-01
(1 row)

As you see leading zeros are properly added in output date.

Share:
11,405
Lucas Kauffman
Author by

Lucas Kauffman

I'm a Belgian security consultant living in Singapore, I'm here to learn and help others out. Opinions are my own. Advice provided with no warranty. Find me on http://cloud101.eu Sometimes you can have a craving only hands can satisfy!

Updated on July 24, 2022

Comments

  • Lucas Kauffman
    Lucas Kauffman almost 2 years

    I want to insert a date into my pg database, but I haven't got a clue what the correct format is and the pg help isn't really helping.

    I have my date from form in d-m-yyyy format. So leading zeros are omitted.

    How can I insert this correctly, is there a function to add leading zeros (either pg or php) ?