Convert Date with Time Zone

本文介绍了一种在不同时间区间内转换日期时间的方法,并提供了两个实用的函数:fun_exchange_timestamp_tz 和 fun_exchange_date_tz。通过这些函数,可以轻松地将一个时区的日期时间转换为另一个时区。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

On occasion giving a date time with a time zone, we want to get the date time under another time zone, how to do it? Here gives a solution:
FUNCTION fun_exchange_timestamp_tz(pv_timestamp TIMESTAMP,
pv_from_tz VARCHAR2,
pv_to_tz VARCHAR2) RETURN TIMESTAMP
WITH TIME ZONE AS
BEGIN
IF pv_timestamp IS NULL
THEN
RETURN NULL;
END IF;
RETURN from_tz(pv_timestamp, pv_from_tz) at TIME ZONE pv_to_tz;
END fun_exchange_timestamp_tz;

/*exchange the date from one time zone to other time zone.*/
FUNCTION fun_exchange_date_tz(pv_date DATE,
pv_from_tz VARCHAR2,
pv_to_tz VARCHAR2) RETURN DATE AS
BEGIN
RETURN fun_exchange_timestamp_tz(CAST(pv_date AS TIMESTAMP),
pv_from_tz,
pv_to_tz);
END;

We can use those functions as below:
select fun_exchange_timestamp_tz(sysdate,'Asia/Shanghai','GMT') from dual;

select fun_exchange_date_tz(sysdate,'Asia/Shanghai','GMT') from dual;

These sqls convert sysdate from time zone 'Asia/Shanghai' time zone ‘GMT’.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值