创建add_months方法

--create or replace function add_months
create or replace function add_months(timestamp, int) returns timestamp as $$  
declare  
  i interval := ($2 || 'month');  
  d1 date := date(to_timestamp($1::text,'yyyy-mm') + interval '1 month' - interval '1 day');  
  d2 date := date($1);  
  res timestamp;  
begin  
  select case when d1=d2 then ((to_char($1+i+interval '1 month', 'yyyy-mm')||'-01')::date - 1) + $1::time else $1+i end into res;  
  return res;  
end;  
$$ language plpgsql strict;  
  
create or replace function add_months(timestamptz, int) returns timestamptz as $$  
declare  
  i interval := ($2 || 'month');  
  d1 date := date(to_timestamp($1::text,'yyyy-mm') + interval '1 month' - interval '1 day');  
  d2 date := date($1);  
  res timestamptz;  
begin  
  select case when d1=d2 then ((to_char($1+i+interval '1 month', 'yyyy-mm')||'-01')::date - 1) + $1::timetz else $1+i end into res;  
  return res;  
end;  
$$ language plpgsql strict;  

相关文章:

  • 2021-10-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-20
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-03-02
  • 2022-12-23
  • 2021-08-10
  • 2021-09-28
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案