Reputation: 139
I have a PostgreSQL table with a Month attribute and Year attribute. They are both formatted as Strings... that is: September, October, November, December and '2016','2017','2018'. How can I combine these month and year columns into a Date column that has the format YYYY/MM/01 indicating the first day of the month.
Upvotes: 1
Views: 1596
Reputation: 23676
First step: Generating a date
with to_date()
: Identifier Month
detects the long month name
SELECT to_date(year || '-' || month || '-01', 'yyyy-Month-dd')
Second step: Generating your expected date format with to_char()
SELECT
to_char(
to_date(year || '-' || month || '-01', 'yyyy-Month-dd'),
'yyyy/MM/dd'
)
FROM mydates
Upvotes: 2