Oracle【低版本实现UTC格式转换】

Oracle低版本实现UTC格式转换


文章目录


前言

现在Oracle版本过低是不能直接转UTC格式时间的,那么我们低版本用户可以以下这样做


一、创建 FUNCTION(GET_UTC_DATE)

sql 复制代码
CREATE OR REPLACE FUNCTION GET_UTC_DATE (V_DATE IN DATE)
    RETURN VARCHAR2
IS
    V_DATA   VARCHAR2 (50);
BEGIN
    SELECT CASE
               --跨年
               WHEN     TO_CHAR (V_DATE, 'MM') = 01
                    AND TO_CHAR (V_DATE, 'DD') = 01
                    AND TO_CHAR (V_DATE, 'HH24') < 08
               THEN
                      TO_NUMBER (TO_CHAR (V_DATE, 'YYYY')) - 1
                   || '-'
                   || LPAD (TO_NUMBER (TO_CHAR (V_DATE, 'MM')) + 11, 2, 0)
                   || '-'
                   || LPAD (TO_NUMBER (TO_CHAR (V_DATE, 'DD')) + 29, 2, 0)
                   || 'T'
                   || LPAD (TO_NUMBER (TO_CHAR (V_DATE, 'HH24')) + 16, 2, 0)
                   || TO_CHAR (V_DATE, ':MI:SS')
                   || '+08:00'
               --闰年跨月
               WHEN     TO_CHAR (V_DATE, 'DD') = 01
                    AND TO_CHAR (V_DATE, 'HH24') < 08
                    AND LPAD (TO_NUMBER (TO_CHAR (V_DATE, 'MM')), 2, 0) = 03
                    AND MOD (TO_CHAR (V_DATE, 'YYYY'), 4) = 0
                    AND MOD (TO_CHAR (V_DATE, 'YYYY'), 100) != 0
               THEN
                      TO_NUMBER (TO_CHAR (V_DATE, 'YYYY'))
                   || '-'
                   || LPAD (TO_NUMBER (TO_CHAR (V_DATE, 'MM')) - 1, 2, 0)
                   || '-'
                   || LPAD (TO_NUMBER (TO_CHAR (V_DATE, 'DD')) + 28, 2, 0)
                   || 'T'
                   || LPAD (TO_NUMBER (TO_CHAR (V_DATE, 'HH24')) + 16, 2, 0)
                   || TO_CHAR (V_DATE, ':MI:SS')
                   || '+08:00'
               --平年跨月
               WHEN        TO_CHAR (V_DATE, 'DD') = 01
                       AND TO_CHAR (V_DATE, 'HH24') < 08
                       AND LPAD (TO_NUMBER (TO_CHAR (V_DATE, 'MM')), 2, 0) =
                           03
                       AND MOD (EXTRACT (YEAR FROM V_DATE), 4) <> 0
                    OR (    MOD (EXTRACT (YEAR FROM V_DATE), 100) = 0
                        AND MOD (EXTRACT (YEAR FROM V_DATE), 400) <> 0)
               THEN
                      TO_NUMBER (TO_CHAR (V_DATE, 'YYYY'))
                   || '-'
                   || LPAD (TO_NUMBER (TO_CHAR (V_DATE, 'MM')) - 1, 2, 0)
                   || '-'
                   || LPAD (TO_NUMBER (TO_CHAR (V_DATE, 'DD')) + 27, 2, 0)
                   || 'T'
                   || LPAD (TO_NUMBER (TO_CHAR (V_DATE, 'HH24')) + 16, 2, 0)
                   || TO_CHAR (V_DATE, ':MI:SS')
                   || '+08:00'
               --跨月/31天
               WHEN     TO_CHAR (V_DATE, 'DD') = 01
                    AND TO_CHAR (V_DATE, 'HH24') < 08
                    AND LPAD (TO_NUMBER (TO_CHAR (V_DATE, 'MM')), 2, 0) IN
                            ('02',
                             '04',
                             '06',
                             '09',
                             '11')
               THEN
                      TO_NUMBER (TO_CHAR (V_DATE, 'YYYY'))
                   || '-'
                   || LPAD (TO_NUMBER (TO_CHAR (V_DATE, 'MM')) - 1, 2, 0)
                   || '-'
                   || LPAD (TO_NUMBER (TO_CHAR (V_DATE, 'DD')) + 30, 2, 0)
                   || 'T'
                   || LPAD (TO_NUMBER (TO_CHAR (V_DATE, 'HH24')) + 16, 2, 0)
                   || TO_CHAR (V_DATE, ':MI:SS')
                   || '+08:00'
               --跨月/30天
               WHEN     TO_CHAR (V_DATE, 'DD') = 01
                    AND TO_CHAR (V_DATE, 'HH24') < 08
                    AND LPAD (TO_NUMBER (TO_CHAR (V_DATE, 'MM')), 2, 0) IN
                            ('01',
                             '05',
                             '07',
                             '08',
                             '10',
                             '12')
               THEN
                      TO_NUMBER (TO_CHAR (V_DATE, 'YYYY'))
                   || '-'
                   || LPAD (TO_NUMBER (TO_CHAR (V_DATE, 'MM')) - 1, 2, 0)
                   || '-'
                   || LPAD (TO_NUMBER (TO_CHAR (V_DATE, 'DD')) + 29, 2, 0)
                   || 'T'
                   || LPAD (TO_NUMBER (TO_CHAR (V_DATE, 'HH24')) + 16, 2, 0)
                   || TO_CHAR (V_DATE, ':MI:SS')
                   || '+08:00'
               --跨日
               WHEN TO_CHAR (V_DATE, 'HH24') < 08
               THEN
                      TO_CHAR (V_DATE, 'YYYY-MM')
                   || '-'
                   || LPAD (TO_NUMBER (TO_CHAR (V_DATE, 'DD')) - 1, 2, 0)
                   || 'T'
                   || LPAD (TO_NUMBER (TO_CHAR (V_DATE, 'HH24')) + 16, 2, 0)
                   || TO_CHAR (V_DATE, ':MI:SS')
                   || '+08:00'
               --当日
               ELSE
                      TO_CHAR (V_DATE, 'YYYY-MM-DD')
                   || 'T'
                   || LPAD (TO_NUMBER (TO_CHAR (V_DATE, 'HH24')) - 8, 2, 0)
                   || TO_CHAR (V_DATE, ':MI:SS')
                   || '+08:00'
           END
               timestamp
      INTO V_DATA
      FROM DUAL;

    RETURN V_DATA;
EXCEPTION
    WHEN OTHERS
    THEN
        RETURN NULL;
END;

二、使用步骤

1.跨年日期

sql 复制代码
--代码如下(示例)
SELECT GET_UTC_DATE (
           TO_DATE ('2023-01-01 07:55:55', 'YYYY-MM-DD HH24:MI:SS'))
  FROM DUAL;

--  out: 2022-12-31T23:55:55+08:00

2.跨月日期(判断 闰年 or 平年、用于计算2月天数,正常大小月)

sql 复制代码
--代码如下(示例)
--跨月日期(判断 闰年 or 平年、用于计算2月天数)
--平年
SELECT GET_UTC_DATE (
           TO_DATE ('2023-03-01 07:55:55', 'YYYY-MM-DD HH24:MI:SS'))
  FROM DUAL;

--  out: 2023-02-28T23:55:55+08:00

--闰年
SELECT GET_UTC_DATE (
           TO_DATE ('2024-03-01 07:55:55', 'YYYY-MM-DD HH24:MI:SS'))
  FROM DUAL;

--  out: 2024-02-29T23:55:55+08:00

--大月
SELECT GET_UTC_DATE (
           TO_DATE ('2023-04-01 07:55:55', 'YYYY-MM-DD HH24:MI:SS'))
  FROM DUAL;

--  out: 2023-03-31T23:55:55+08:00

--小月
SELECT GET_UTC_DATE (
           TO_DATE ('2023-05-01 07:55:55', 'YYYY-MM-DD HH24:MI:SS'))
  FROM DUAL;

--  out: 2023-04-30T23:55:55+08:00

3.跨日日期

sql 复制代码
SELECT GET_UTC_DATE (
           TO_DATE ('2023-04-02 07:55:55', 'YYYY-MM-DD HH24:MI:SS'))
  FROM DUAL;

--  out: 2023-04-01T23:55:55+08:00

3.正常日期

sql 复制代码
SELECT GET_UTC_DATE (
           TO_DATE ('2023-04-02 09:55:55', 'YYYY-MM-DD HH24:MI:SS'))
  FROM DUAL;

--  out: 2023-04-02T01:55:55+08:00

总结

这篇文章到这就结束了,感受您的观看,真诚的致谢!!

相关推荐
锐策6 分钟前
〔 MySQL 〕数据类型
数据库·mysql
天天要nx27 分钟前
D68【python 接口自动化学习】- python基础之数据库
数据库·python
Allen Bright30 分钟前
Redis安装
数据库·redis·缓存
门牙咬脆骨36 分钟前
【MYSQL】数据库日志 (了解即可)
数据库·mysql
ModelBulider37 分钟前
十三、注解配置SpringMVC
java·开发语言·数据库·sql·mysql
豆 腐1 小时前
MySQL【四】
android·数据库·笔记·mysql
熊明才1 小时前
Ubuntu 22.04.4 LTS + certbot 做自动续签SSL证书(2024-11-14亲测)
数据库·网络协议·ssl
sagima_sdu2 小时前
Py2Neo 库将 Json 文件导入 Neo4J
oracle·json·neo4j
用户6279947182622 小时前
GBase 8a MPP Cluster基于审计日志的冷温热数据分析
数据库
用户6279947182622 小时前
快速搭建南大通用GBase 8s数据库SSC共享存储集群
数据库