首页 > 其他 > 详细

postgres add_months方法

时间:2021-01-12 23:18:50      阅读:33      评论:0      收藏:0      [点我收藏+]

创建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;  

postgres add_months方法

原文:https://www.cnblogs.com/Alicebat/p/14269433.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!