postgresql 实现计算日期间隔排除周末节假日方案

文章介绍了如何在PostgreSQL中创建一个名为`get_timedelay`的函数,用于计算两个timestampwithtimezone之间的间隔,同时考虑周末和节假日的影响。函数通过查询假日表来确定工作日和非工作日的时长差异。

前置条件:需要维护一张节假日日期表。例如创建holiday表保存当年假期日期

CREATE TABLE `holiday` (
	`id` BIGINT(10) ZEROFILL NOT NULL DEFAULT 0,
	`day` TIMESTAMP NULL DEFAULT NULL,
	PRIMARY KEY (`id`)
)
COMMENT='假期表'
COLLATE='utf8mb4_0900_ai_ci'
;

返回日期为xx日xx时xx分格式,可以在此基础上调整格式

-- FUNCTION: public.get_timedelay(timestamp with time zone, timestamp with time zone)

-- DROP FUNCTION IF EXISTS public.get_timedelay(timestamp with time zone, timestamp with time zone);

CREATE OR REPLACE FUNCTION public.get_timedelay(
	starttime timestamp with time zone,
	endtime timestamp with time zone)
    RETURNS text
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
AS $BODY$
DECLARE
	 v_return  varchar;--返回间隔时间 xx日xx时xx分
	 v_minute  integer;--间隔分钟
	 v_hour integer;
	 v_temp_minute integer;
	 v_temp_hour 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值