How to get the number of days in a month?

后端 未结 6 2047
梦毁少年i
梦毁少年i 2020-12-30 23:25

I am trying to get the following in Postgres:

select day_in_month(2);

Expected output:

28

Is there any bu

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-30 23:50

    SELECT cnt_dayofmonth(2016, 2);  -- 29
    
    
    create or replace function cnt_dayofmonth(_year int, _month int)
    returns int2 as
    $BODY$
    -- ZU 2017.09.15, returns the count of days in mounth, inputs are year and month
    declare 
        datetime_start date := ('01.01.'||_year::char(4))::date;
        datetime_month date := ('01.'||_month||'.'||_year)::date;
            cnt int2;
    begin 
      select extract(day from (select (datetime_month + INTERVAL '1 month -1 day'))) into cnt;
    
      return cnt;
    end;
    $BODY$
    language plpgsql;
    

提交回复
热议问题