How to get the last day of month in postgres?

前端 未结 5 1440
醉话见心
醉话见心 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:48

    Okay, so you've got a numeric(18) column containing numbers like 20150118. You can convert that to a date like:

    to_date(your_date_column::text, 'YYYYMMDD')
    

    From a date, you can grab the last day of the month like:

    (date_trunc('month', your_date_column) + 
        interval '1 month' - interval '1 day')::date;
    

    Combined, you'd get:

    select  (date_trunc('month', to_date(act_dt::text, 'YYYYMMDD')) + 
               interval '1 month' - interval '1 day')::date
    from    YourTable;
    

    Example at SQL Fiddle.

提交回复
热议问题