How to get the number of days in a month?

后端 未结 6 2032
梦毁少年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:39

    You can write a function:

    CREATE OR REPLACE FUNCTION get_total_days_in_month(timestamp)
    RETURNS decimal
    IMMUTABLE
    AS $$
      select cast(datediff(day, date_trunc('mon', $1), last_day($1) + 1) as decimal)
    $$ LANGUAGE sql;
    

提交回复
热议问题