How to find the last day os the month in postgres? I have a date columns stored as numeric(18) in the format(YYYYMMDD) I am trying it to make it date using
to_date("act_dt",'YYYYMMDD') AS "act date"
then find the last day of this date: like this:
(select (date_trunc('MONTH',to_date("act_dt",'YYYYMMDD')) + INTERVAL '1 MONTH - 1 day')::date)
but it gives me this error:
ERROR: Interval values with month or year parts are not supported
Detail:
-----------------------------------------------
error: Interval values with month or year parts are not supported
code: 8001
context: interval months: "1"
query: 673376
location: cg_constmanager.cpp:145
process: padbmaster [pid=20937]
-----------------------------------------------
Any help?
Postgres version:
PostgreSQL 8.0.2 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.4.2 20041017 (Red Hat 3.4.2-6.fc3), Redshift 1.0.874
to_date
converts only strings to dates. If your column contains Unix epoch values, you need to useto_timestamp
:to_timestamp(act_dt)::date
. – Zwickto_timestamp
expects double precision type, try casting your column to it:to_timestamp(act_dt::float8)::date
. BTW, could you confirm that this column stores Unix epoch time values? If this is not the case, you should provide explanation in your question what exactly is it storing. – ZwickINTERVAL '1 MONTH - 1 day'
byINTERVAL '1 MONTH' - interval '1 day'
– Arhna20131014 20130614 20150124 20110128 20120825
– Warison