How to get the last day of month in postgres?

前端 未结 5 1438
醉话见心
醉话见心 2020-12-18 17:47

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

         


        
5条回答
  •  無奈伤痛
    2020-12-18 18:44

    For anybody coming to this question looking for the Postgres way to do this (not using Redshift), here's how you'd do it:

    SELECT (date_trunc('month', '2017-01-05'::date) + interval '1 month' - interval '1 day')::date
    AS end_of_month;
    

    Replacing the '2017-01-05' with whatever date you want to use. You can make this into a function like this:

    create function end_of_month(date)
    returns date as
    $$
    select (date_trunc('month', $1) + interval '1 month' - interval '1 day')::date;
    $$ language 'sql'
    immutable strict;
    

提交回复
热议问题