Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Postgres Amend Current date from mmyyyy to mm-yyyy

I have a table within postgres which has a month year column formatted as mmyyyy, how do I go about either adding a new column, or amending the exisiting column so I can have the format as mm-yyyy please?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can convert it to a proper date (at the start of the month), then format that date back the way you want it:

to_char(to_date(the_column, 'mmyyyy'), 'mm-yyyy')

You can use that in a SELECT query, create view or UPDATE the column using that expression.

Alternatively use a simple string manipulation:

left(the_column, 2)||'-'||right(the_column, 4)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading