How to format a date column into just the month?
I am trying to format a date column into only the month.
I would need the final value in some sort of date datatype so that I can order the results by date/time, not order by text. So that February sorts after January etc.
select TO_DATE(TO_CHAR(purchase_date), 'Month')
from transactions
order by 1;
The above produces an error "Invalid value for Month"
Any ideas?
I would need the final value in some sort of date datatype so that I can order the results by date/time, not order by text. So that February sorts after January etc.
select TO_DATE(TO_CHAR(purchase_date), 'Month')
from transactions
order by 1;
The above produces an error "Invalid value for Month"
Any ideas?
0
Comments
=> select date_part('month', purchase_date) from pdate;
date_part
-----------
4
1
(2 rows)
=> select month(purchase_date) from pdate;
month
-------
4
1
(2 rows)
Perhaps this feature does not yet exist?
=> create function tomonthname(x int) return varchar(20) as begin return (case when (x = '1') then 'January' when (x = '2') then 'February' ELSE 'no month' end); end;
CREATE FUNCTION
=> select tomonthname(month(purchase_date)) from pdate;
tomonthname
-------------
January
January
(2 rows)