Postgres自定义函数,格式化日期 年-周,年-非自然月

52万条数据耗时3秒左右

1.年-周

-- 年-周
create or replace function year_week(datetime timestamp) returns varchar
    language plpgsql
as
$$
declare
    year int;
    week int;
    day  int;
begin
    year := extract(year from datetime);
    week := extract(week from datetime);
    day := extract(day from datetime);

    if day < 6 and week > 50 then
        return concat(year - 1, '-', week);
    elseif day > 360 and week < 2 then
        return concat(year + 1, '-', week);
    else
        return concat(year, '-', week);
    end if;
end;
$$;
alter function year_week(datetime timestamp) owner to postgres;

-- 年-周
create or replace function year_week(datetime date) returns varchar
    language plpgsql
as
$$
declare
    year int;
    week int;
    day  int;
begin
    year := extract(year from datetime);
    week := extract(week from datetime);
    day := extract(day from datetime);

    if day < 6 and week > 50 then
        return concat(year - 1, '-', week);
    elseif day > 360 and week < 2 then
        return concat(year + 1, '-', week);
    else
        return concat(year, '-', week);
    end if;
end;
$$;
alter function year_week(datetime date) owner to postgres;

2.年-非自然月 (timestamp 字段类型)

如果本月的X号开始,则month_start_day 为 正数,例如每个月的起始日是3号,则 month_start_day = 3
如果上月的X号开始,则month_start_day 为 负数,例如每个月的起始日是上月25号,则 month_start_day = -25


-- 自定义月开始日的 年-月
create or replace function year_month(datetime date, month_start_day int) returns character varying
    language plpgsql
as
$$
declare
    year  int;
    month int;
    day   int;
begin
    if month_start_day isnull or month_start_day = 0 or month_start_day = 1 then
        return to_char(datetime, 'YYYY-MM');
    end if;

    year := extract(year from datetime);
    month := extract(month from datetime);
    day := extract(day from datetime);

    if month_start_day < 0 and day < -month_start_day then
        return concat(year, '-', to_char(month, 'FM00'));
    elseif month_start_day < 0 and day >= -month_start_day then
        return to_char(date_trunc('MONTH', datetime) + interval '1 MONTH', 'YYYY-MM');
    elseif month_start_day > 1 and day < month_start_day then
        return to_char(date_trunc('MONTH', datetime) - interval '1 MONTH', 'YYYY-MM');
    elseif month_start_day > 1 and day >= month_start_day then
        return concat(year, '-', to_char(month, 'FM00'));
    else
        return to_char(datetime, 'YYYY-MM');
    end if;
end;
$$;

alter function year_month(date, int) owner to postgres;

-- 自定义月开始日的 年-月
create or replace function year_month(datetime timestamp, month_start_day int) returns character varying
    language plpgsql
as
$$
declare
    year  int;
    month int;
    day   int;
begin
    if month_start_day isnull or month_start_day = 0 or month_start_day = 1 then
        return to_char(datetime, 'YYYY-MM');
    end if;

    year := extract(year from datetime);
    month := extract(month from datetime);
    day := extract(day from datetime);

    if month_start_day < 0 and day < -month_start_day then
        return concat(year, '-', to_char(month, 'FM00'));
    elseif month_start_day < 0 and day >= -month_start_day then
        return to_char(date_trunc('MONTH', datetime) + interval '1 MONTH', 'YYYY-MM');
    elseif month_start_day > 1 and day < month_start_day then
        return to_char(date_trunc('MONTH', datetime) - interval '1 MONTH', 'YYYY-MM');
    elseif month_start_day > 1 and day >= month_start_day then
        return concat(year, '-', to_char(month, 'FM00'));
    else
        return to_char(datetime, 'YYYY-MM');
    end if;
end;
$$;

alter function year_month(date, int) owner to postgres;

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农20年

给我你就上当了

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值