南大通用数据库(Gbase 8s) 创建UDR外部函数

news/2025/2/21 8:16:04/

一、在使用 date_format、from_unixtime、to_days、yearweek 函数时,Gbase 8s 数据库不支持,可以使用创建 UDR 外部函数来实现

二、登录命令控制台或者使用 navicat 连接 Gbase 数据库

这里使用 navicat ,点击新增连接选择 PostGreSql 驱动,添加地址、账号、密码
连接数据库后,选中目标库选中目标模式,再点击函数-新增函数执行以下语句即可

注意:这里 选中 public 模式,使用 mss 用户,自行修改函数中对应的内容( 例如:FUNCTION “public”.“date_format”、OWNER TO “mss”)

  1. date_format 函数

    CREATE OR REPLACE FUNCTION "public"."date_format"("ctimestamp" timestamptz, "informate" varchar)RETURNS "pg_catalog"."varchar" AS $BODY$-- Routine body goes here...DECLARE  result_current_date varchar;BEGIN
    --             IF upper($1) = upper('YYYY-MM-DD') || upper($1) = upper('%Y-%M-%D') THEN
    --                     SELECT to_char(now(),'YYYY-MM-DD') into result_current_date;
    --             END IF;
    --     
    --             
    --             IF upper($1) = upper('%Y-%M-%D %h:%m') || upper($1) = upper('%Y-%M-%D %h:%m') THEN
    --                     SELECT to_char(now(),'YYYY-MM-DD HH:mm') into result_current_date;
    --             END IF;
    --             
    --           IF upper($1) = upper('%Y-%M-%D %h:%m:%s') || upper($1) = upper('%Y-%M-%D %h:%m:%s') THEN
    --                     SELECT to_char(now(),'YYYY-MM-DD HH:mm:ss') into result_current_date;
    --             END IF;case upper($2)when upper('%Y') thenSELECT to_char(smalldatetime_to_timestamp($1),'YYYY') into result_current_date;when upper('%Y-%M') thenSELECT to_char(smalldatetime_to_timestamp($1),'YYYY-MM') into result_current_date;                    when upper('%Y-%M-%D') thenSELECT to_char(smalldatetime_to_timestamp($1),'YYYY-MM-DD') into result_current_date;when upper('%Y-%M-%D %h') thenSELECT to_char(smalldatetime_to_timestamp($1),'YYYY-MM-DD HH24') into result_current_date;when upper('%Y-%M-%D %h:%m') thenSELECT to_char(smalldatetime_to_timestamp($1),'YYYY-MM-DD HH24:MI') into result_current_date;when upper('%Y-%M-%D %h:%m:%s') thenSELECT to_char(smalldatetime_to_timestamp($1),'YYYY-MM-DD HH24:MI:ss') into result_current_date;            when upper('%M') thenSELECT to_char(smalldatetime_to_timestamp($1),'MM') into result_current_date;                when upper('%M-%D') thenSELECT to_char(smalldatetime_to_timestamp($1),'MM-DD') into result_current_date;when upper('%D') thenSELECT to_char(smalldatetime_to_timestamp($1),'DD') into result_current_date;when upper('%h') thenSELECT to_char(smalldatetime_to_timestamp($1),'HH24') into result_current_date;    when upper('%h:%m') thenSELECT to_char(smalldatetime_to_timestamp($1),'HH24:MI') into result_current_date;            when upper('%m') thenSELECT to_char(smalldatetime_to_timestamp($1),'MI') into result_current_date;                            when upper('%m:%s') thenSELECT to_char(smalldatetime_to_timestamp($1),'MI:ss') into result_current_date;        when upper('%s') thenSELECT to_char(smalldatetime_to_timestamp($1),'ss') into result_current_date;                                                                when upper('%h:%m:%s') thenSELECT to_char(smalldatetime_to_timestamp($1),'HH24:MI:ss') into result_current_date;            elseSELECT to_char(smalldatetime_to_timestamp($1),informate) into result_current_date;end case;RETURN result_current_date;
    END$BODY$LANGUAGE plpgsql VOLATILECOST 100;ALTER FUNCTION "public"."date_format"("ctimestamp" timestamptz, "informate" varchar) OWNER TO "mss";
    

    查询语句:

    SELECT date_format(now(),'%Y-%M-%D %h:%m:%s');
    
  2. from_unixtime 函数

    CREATE OR REPLACE FUNCTION "public"."from_unixtime"("t" int8)RETURNS "pg_catalog"."timestamp" AS $BODY$DECLARE  result_current_date timestamp;BEGIN  select TO_TIMESTAMP(t) into result_current_date;RETURN result_current_date;
    END; $BODY$LANGUAGE plpgsql VOLATILECOST 100;ALTER FUNCTION "public"."from_unixtime"("t" int8) OWNER TO "mss";
    

    查询语句:

    select from_unixtime(1692328679);
    
  3. to_days 函数

    -- 参数 varchar类型
    CREATE OR REPLACE FUNCTION "public"."to_days"("ctimestamp" varchar)RETURNS "pg_catalog"."int4" AS $BODY$-- Routine body goes here...DECLARE  result_current_date int4;BEGINSELECT TIMESTAMPDIFF(day, '0001-01-01', $1) into result_current_date;RETURN result_current_date;
    END$BODY$LANGUAGE plpgsql VOLATILECOST 100;ALTER FUNCTION "public"."to_days"("ctimestamp" varchar) OWNER TO "mss";-- 参数 timestamptz 类型
    CREATE OR REPLACE FUNCTION "public"."to_days"("ctimestamp" timestamptz)RETURNS "pg_catalog"."int4" AS $BODY$-- Routine body goes here...DECLARE  result_current_date int4;BEGINSELECT TIMESTAMPDIFF(day, '0001-01-01', $1) into result_current_date;RETURN result_current_date;
    END$BODY$LANGUAGE plpgsql VOLATILECOST 100;ALTER FUNCTION "public"."to_days"("ctimestamp" timestamptz) OWNER TO "mss";
    

    查询语句:

    select to_days(now());
    
  4. yearweek 函数

    CREATE OR REPLACE FUNCTION "public"."yearweek"("ctimestamp" timestamptz)RETURNS "pg_catalog"."int4" AS $BODY$-- Routine body goes here...DECLARE  week_n int4;year_n int4;BEGINSELECT to_char(smalldatetime_to_timestamp($1),'YYYY') into year_n;SELECT trunc(1 + (smalldatetime_to_timestamp($1) - TRUNC(smalldatetime_to_timestamp($1), 'YEAR')) / 7) into week_n;RETURN ((year_n*100)+week_n);
    END$BODY$LANGUAGE plpgsql VOLATILECOST 100;ALTER FUNCTION "public"."yearweek"("ctimestamp" timestamptz) OWNER TO "mss";
    

    查询语句:

    select YEARWEEK(now());
    select YEARWEEK('2023-01-03 12');
    

http://www.ppmy.cn/news/1043440.html

相关文章

vue3 ref的使用、问题及源码分析;引用型变量和原始类型变量的复制值

文章目录 ref定义及作用用法源码 实验一 修改原变量和ref后的值原始数据类型对象类型总结 实验二 props的ref ref定义及作用 可以将 ref 看成 reactive 的一个变形版本,这是由于 reactive 内部采用 Proxy 来实现,而 Proxy 只接受对象作为入参&#xff0…

blender的快捷键记录

按键作用备注R旋转物体移动、旋转或缩放物体时,按下X、Y或Z键:按X、Y或Z轴方向移动、旋转或缩放S缩放物体G移动物体TAB键切换为编辑模式CTRL A弹出应用菜单物体模式旋转缩放后应用旋转与缩放,再进入编辑模式SHIFT 鼠标右键移动游标位置SHIF…

fnn手动实现和nn实现(包括3种激活函数、隐藏层)

原文网址:https://blog.csdn.net/m0_52910424/article/details/127819278 fnn手动实现: import time import matplotlib.pyplot as plt import numpy as np import torch import torch.nn as nn import torchvision from torch.nn.functional import cross_entrop…

[管理与领导-28]:IT基层管理者 - 团队管理 - 育人 - 绩效面谈

目录 一、什么是绩效面谈 二、教练式绩效面谈 三、量化的绩效 四、研发KPI 一、什么是绩效面谈 绩效面谈(Performance Review)是管理者和员工之间进行定期讨论和评估员工工作表现的一种沟通方式。 通过绩效面谈,管理者可以与员工一起回…

QT的network的使用

一个简单的双向的网络连接和信息发送。效果如下图所示: 只需要配置这个主机的IP和端口号,由客户端发送链接请求。即可进行连接。 QT的network模块是一个用于网络编程的模块,它提供了一系列的类和函数,可以让您使用TCP/IP协议来创…

CSS 背景属性

前言 背景属性 属性说明background-color背景颜色background-image背景图background-repeat背景图平铺方式background-position背景图位置background-size背景图缩放background-attachment背景图固定background背景复合属性 背景颜色 可以使用background-color属性来设置背景…

SQL力扣练习(十一)

目录 1.树节点(608) 示例 1 解法一(case when) 解法二(not in) 2.判断三角形(610) 示例 1 解法一(case when) 解法二(if) 解法三(嵌套if) 3.只出现一次的最大数字(619) 示例 1 解法一(count limit) 解法二(max) 4.有趣的电影(620) 解法一 5.换座位(626) 示例 …

jQuery第一次接触

jQuery是一个轻量级js库 1.下载jquery库&#xff0c;网址Download jQuery | jQuery npm i jquery 2.还可以从cdn中载入jquery <script src"https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> 3.j代表js&#xff0c;query代表查询&#xff0c;jQu…