PostgreSQL实现dateadd函数

来源:未知 责任编辑:责任编辑 发表时间:2015-10-08 14:16 点击:

PostgreSQL实现dateadd函数
 
Sql代码  
CREATE OR REPLACE FUNCTION timeadd(difftype character varying, incrementvalue integer, inputdate timestamp with time zone)  
  RETURNS timestamp without time zone AS  
$BODY$  www.2cto.com  
DECLARE
   YEAR_CONST Char(15) := 'year';  
   MONTH_CONST Char(15) := 'month';  
   WEEK_CONST Char(15) := 'week';  
   DAY_CONST Char(15) := 'day';  
   HOUR_CONST Char(15) := 'hour';  
   MIN_CONST Char(15) :='mi';  
   SECOND_CONST Char(15) :='ss';  
   dateTemp timestamp;  
   intervals interval;  
BEGIN 
   IF lower($1) = lower(YEAR_CONST) THEN  
       select cast(cast(incrementvalue as character varying) || ' year' as interval) into intervals;    www.2cto.com  
   ELSEIF lower($1) = lower(MONTH_CONST) THEN  
       select cast(cast(incrementvalue as character varying) || ' months' as interval) into intervals;  
 
   ELSEIF lower($1) = lower(WEEK_CONST) THEN  
       select cast(cast(incrementvalue as character varying) || ' week' as interval) into intervals;  
   ELSEIF lower($1) = lower(DAY_CONST) THEN  
       select cast(cast(incrementvalue as character varying) || ' day' as interval) into intervals;  
   ELSEIF lower($1) = lower(HOUR_CONST) THEN  
       select cast(cast(incrementvalue as character varying) || ' hour' as interval) into intervals;  
   ELSEIF lower($1) = lower(MIN_CONST) THEN  
       select cast(cast(incrementvalue as character varying) || ' minute' as interval) into intervals;  
   ELSEIF lower($1) = lower(SECOND_CONST) THEN  
       select cast(cast(incrementvalue as character varying) || ' second' as interval) into intervals;     
   END IF;  
  
   dateTemp:= inputdate + intervals;  
  
   RETURN dateTemp;  
END;  
$BODY$  
  LANGUAGE 'plpgsql' VOLATILE;  
   www.2cto.com  
------------------------------------------- API ------------------------------------------------------------
功能:类似于Sybase语法中的dateadd函数,用于时间加减相应的时间段
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码:点击我更换图片
最新评论 更多>>

推荐热点

  • Request.ServerVariables 参数大全
  • 查看sql修改痕迹(SQL Change Tracking on Table)
  • 写给MongoDB开发者的50条建议Tip1
  • Percolator与分布式事务思考(二)
  • App数据层设计及云存储使用指南
  • PostgreSQL启动过程中的那些事三:加载GUC参数
  • SQL Server、Oracle、db2所提供的简装版(Express)比较
  • PostgreSQL 安装问题
  • 导入excel文件处理流程节点的解决方案
网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
Copyright © 2008-2015 计算机技术学习交流网. 版权所有

豫ICP备11007008号-1