iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >【小计】PostgreSQL实现Oracle的trunc日期函数功能
  • 444
分享到

【小计】PostgreSQL实现Oracle的trunc日期函数功能

2024-04-02 19:04:59 444人浏览 泡泡鱼
摘要

create or replace function trunc(p_timestamp timestamp with time&nbs


create or replace function trunc(p_timestamp timestamp with time zone, p_fORMart varchar default 'DD')
 returns timestamp without time zone as
$$
declare
 v_timestamp timestamp := null;
 v_formart varchar(10) := upper(p_formart);
begin
 
 if p_timestamp is not null then
  if v_formart in ('YYYY', 'YEAR') then
   -- 当前年的第一天(YYYY-01-01 00:00:00)
   v_timestamp := date_trunc('year', p_timestamp);
  elsif v_formart in ('MONTH', 'MON', 'MM', 'RM') then
   -- 当前月第一天(YYYY-MM-01 00:00:00)
   v_timestamp := date_trunc('month', p_timestamp);
  elsif v_formart in ('DD', 'DAY', 'DY') then
   -- 当天(YYYY-MM-DD 00:00:00)
   v_timestamp := date_trunc('day', p_timestamp);
  elsif v_formart = 'D' then
   -- 当前周第一天[周日为第一天](YYYY-MM-DD 00:00:00)
   v_timestamp := (date_trunc('WEEK', p_timestamp) - interval'1 day');
  elsif v_formart in ('W1', 'W2', 'W3', 'W4', 'W5', 'W6', 'W7') then
   -- 当前周第几天[周日为第一天](YYYY-MM-DD 00:00:00)
   v_timestamp := date_trunc('WEEK', p_timestamp)::date + substr(v_formart, 2, 1)::integer - 2;
  elsif v_formart ~ '^D\+?[0-9]*$' then
   -- 当年第几天(YYYY-MM-DD 00:00:00)
   if substr(v_formart, 2, length(v_formart)-1)::integer between 1 and 366 then
    v_timestamp := date_trunc('year', p_timestamp)::date + substr(v_formart, 2, length(v_formart)-1)::integer - 1;
    if date_trunc('year', v_timestamp)::date > date_trunc('year', p_timestamp)::date then
     v_timestamp := date_trunc('year', v_timestamp)::date - interval'1 day';
    end if;
   else
    raise exception 'U-2001 [%] is not recognize. please enter "D[1~366]"', p_formart;
   end if;
  elsif v_formart in ('HH', 'HH24') then
   v_timestamp := date_trunc('hour', p_timestamp);
  elsif v_formart = 'HH12' then
   v_timestamp := to_char(p_timestamp, 'yyyy-mm-dd hh22:00:00')::timestamp;
  elsif v_formart in ('MINUTE', 'MI') then
   v_timestamp := date_trunc('minute', p_timestamp);
  elsif v_formart = 'CC' then
   v_timestamp := to_date((trunc(date_part('years', p_timestamp)::integer/100)*100+1)::varchar, 'yyyy');
  elsif v_formart in ('HELP', '?') then
   raise exception 'U-2001 please enter formart code in ( YYYY|YEAR, MONTH|MON|MM|RM, DD|DAY|DY, D, W[1~7], D[1~366], HH|HH24, HH12, MINUTE|MI, CC )';
  else
   raise exception 'U-2001 [%] is not recognize. you can try [help]', p_formart;
  end if;
 else
  v_timestamp := p_timestamp;
 end if;
 return v_timestamp;
end;
$$
 language plpgsql;

 
-- 测试数据
select trunc(current_date, 'D360'), trunc(current_date, 'D'),trunc(current_date, 'W1');


您可能感兴趣的文档:

--结束END--

本文标题: 【小计】PostgreSQL实现Oracle的trunc日期函数功能

本文链接: https://www.lsjlt.com/news/36452.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作