GaussDB: db2->gaussdb 函数转换
一、db2->gaussdb函数转换
问题描述:使用GaussDB替代DB2的方案,使用起来还是有些差别,做一下函数的映射转换。
|
DB2写法 |
GaussDB改写语法 |
|
|
日期函数 |
days(OUTWORKDATE) |
EXTRACT(epoch from outworkdate) /86400; EXTRACT(DAY FROM (OUTWORKDATE - DATE '0001-01-01' + INTEGER '1' )) |
|
day(OUTWORKDATE) |
EXTRACT(DAY FROM outworkdate); DATE_PART('DAY', OUTWORKDATE) |
|
|
month(current_date) |
EXTRACT(MONTH FROM current_date); DATE_PART('MONTH', TRUNC(CURRENT_DATE)) |
|
|
year(current_date) |
EXTRACT(YEAR FROM outworkdate); DATE_PART('YEAR', TRUNC(CURRENT_DATE)) |
|
|
CURRENT_DATE -1 DAY/MONTH/YEAR |
CURRENT_DATE - INTERVAL '1 day' AS yesterday, CURRENT_DATE - INTERVAL '1 month' AS last_month, CURRENT_DATE - INTERVAL '1 year' AS last_year; TRUNC(CURRENT_DATE) - INTERVAL '1 DAY' AS yesterday, TRUNC(CURRENT_DATE) - INTERVAL '1 MONTH' AS last_month, TRUNC(CURRENT_DATE) - INTERVAL '1 YEAR' AS last_year; |
|
|
CURRENT_DATE -1 DAY -1 MONTH |
current_date - interval '1 DAY' - interval '1 MONTH' |
|
|
to_char(CURRENT_DATE,'yyyy-mm') |
to_char(current_date,'yyyy-mm');to_char(trunc(current_date),'yyyy-mm') |
|
|
TIMESTAMPDIFF(2,END_TIME - START_TIME) |
extract(epoch from (end_time - start_time)); trunc(extract(epoch from (end_time - start_time))); TIMESTAMPDIFF()函数只在mysql模式中可以使用 |
|
|
数据类型转化 |
dec(RATE,10,2) |
rate::numeric(10,2); cast(rate as numeric(10,2)); |
|
int(100.123) |
cast(100.123 as int); 100.123::int; |
|
|
char(1) |
cast(1 as char(1)) |
|
|
to_date() |
to_date() |
|
|
to_char() |
to_char() |
|
|
其他常用函数 |
LOCATE('|','115|56',1) |
INSTR('115|56', '|', 1) |
|
listagg(name,',') |
listagg(b,';') within group (order by b) |
|
|
nvl() |
nvl() |
|
|
trim() |
trim(BOTH 'x' from 'xTommxx'); trim(leading 'x' from 'xTommxx'); trim(trailing 'x' from 'xTommxx'); |
|
|
substr() |
substr('stringtest' from 4); substr('stringtest',4); |
|
|
VALUE() |
COALESCE() |
|
|
floor() |
floor() |
|
|
round() |
round() |
|
|
coalesce(company,0) |
coalesce(company,0) |
|
|
length() |
length() |
|
|
POSSTR(EXP1,EXP2) |
position(exp1,exp2) |
|
|
REPLACE (EXP1,EXP2,EXP3) |
replace(exp1,exp2,exp3) |
二、测试过程
日期函数
days(outworkdate) 函数转换*****************************************************************************
create table employee(id serial primary key,name varchar(100),outworkdate date);
insert into employee(name,outworkdate) values('alice','2023-01-15');
insert into employee(name,outworkdate) values('bob','2023-02-20');
insert into employee(name,outworkdate) values('david','2023-04-05');
insert into employee(name,outworkdate) values('eve','2023-05-25'); testdb_0903=> select name,outworkdate,extract(epoch from outworkdate) / 86400 as days_since_epoch from employee;
name | outworkdate | days_since_epoch
-------+---------------------+------------------
alice | 2023-01-15 00:00:00 | 19372
bob | 2023-02-20 00:00:00 | 19408
david | 2023-04-05 00:00:00 | 19452
eve | 2023-05-25 00:00:00 | 19502
(4 rows) testdb_0903=> select name,outworkdate,extract(day from (outworkdate-date '1970-01-01'+integer '1')) as days_since_epoch from employee;
name | outworkdate | days_since_epoch
-------+---------------------+------------------
alice | 2023-01-15 00:00:00 | 19373
bob | 2023-02-20 00:00:00 | 19409
david | 2023-04-05 00:00:00 | 19453
eve | 2023-05-25 00:00:00 | 19503
(4 rows) day(outworkdate) 函数转换*****************************************************************************
testdb_0903=> select name,outworkdate,extract(day from outworkdate) as day_of_month from employee;
name | outworkdate | day_of_month
-------+---------------------+--------------
alice | 2023-01-15 00:00:00 | 15
bob | 2023-02-20 00:00:00 | 20
david | 2023-04-05 00:00:00 | 5
eve | 2023-05-25 00:00:00 | 25
(4 rows) testdb_0903=>
testdb_0903=>
testdb_0903=> select name,outworkdate,date_part('DAY',outworkdate) as day_of_month from employee;
name | outworkdate | day_of_month
-------+---------------------+--------------
alice | 2023-01-15 00:00:00 | 15
bob | 2023-02-20 00:00:00 | 20
david | 2023-04-05 00:00:00 | 5
eve | 2023-05-25 00:00:00 | 25
(4 rows) month(current_date) 函数转换*****************************************************************************
testdb_0903=> select extract(month from current_date) from dual;
date_part
-----------
9
(1 row) testdb_0903=> select date_part('MONTH',trunc(current_date)) from dual;
date_part
-----------
9
(1 row) year(current_date) 函数转换*****************************************************************************
testdb_0903=> select extract(year from current_date) from dual;
date_part
-----------
2024
(1 row) testdb_0903=>
testdb_0903=> select date_part('YEAR',trunc(current_date)) from dual;
date_part
-----------
2024
(1 row) current_date -1 DAY/MONTH/YEAR 函数转换*****************************************************************************
testdb_0903=> select current_date as today,
current_date - interval '1 day' as yesterday,
current_date - interval '1 month' as last_month,
current_date - interval '1 year' as last_year from dual;
today | yesterday | last_month | last_year
------------+---------------------+---------------------+---------------------
2024-09-29 | 2024-09-28 00:00:00 | 2024-08-29 00:00:00 | 2023-09-29 00:00:00
(1 row) testdb_0903=>
testdb_0903=> select trunc(current_date) - interval '1 DAY' as yesterday,
trunc(current_date) - interval '1 month' as last_month,
trunc(current_date) - interval '1 year' as last_year from dual;
yesterday | last_month | last_year
---------------------+---------------------+---------------------
2024-09-28 00:00:00 | 2024-08-29 00:00:00 | 2023-09-29 00:00:00
(1 row) current_date -1 DAY -1 MONTH 函数转换*****************************************************************************
testdb_0903=> select current_date - interval '1 DAY' - interval '1 MONTH' as result_date from dual;
result_date
---------------------
2024-08-28 00:00:00
(1 row) testdb_0903=>
testdb_0903=> select trunc(current_date) - interval '1 DAY' - interval '1 MONTH' as result_date from dual;
result_date
---------------------
2024-08-28 00:00:00
(1 row) to_char(current_date,'yyyy-mm') 函数转换*****************************************************************************
testdb_0903=> select to_char(current_date,'yyyy-mm') from dual;
to_char
---------
2024-09
(1 row) testdb_0903=> select to_char(trunc(current_date),'yyyy-mm') from dual;
to_char
---------
2024-09
(1 row) timestampdiff(2,end_time-start_time) 函数转换*****************************************************************************
GaussDB同名函数仅在MySQL模式数据库中有效。
testdb_0903=> create table events(id serial primary key,start_time timestamp,end_time timestamp);
testdb_0903=> insert into events(start_time,end_time) values('2023-10-01 10:00:00','2023-10-01 10:30:00');
testdb_0903=> insert into events(start_time,end_time) values('2023-10-01 11:00:00','2023-10-01 12:15:00');
testdb_0903=> insert into events(start_time,end_time) values('2023-10-01 13:00:00','2023-10-01 13:45:00'); testdb_0903=> select id,start_time,end_time,extract(epoch from (end_time - start_time)) as diff_time from events;
id | start_time | end_time | diff_time
----+---------------------+---------------------+-----------
1 | 2023-10-01 10:00:00 | 2023-10-01 10:30:00 | 1800
2 | 2023-10-01 11:00:00 | 2023-10-01 12:15:00 | 4500
3 | 2023-10-01 13:00:00 | 2023-10-01 13:45:00 | 2700
(3 rows) testdb_0903=>
testdb_0903=> select id,start_time,end_time,trunc(extract(epoch from (end_time - start_time))) as diff_time from events;
id | start_time | end_time | diff_time
----+---------------------+---------------------+-----------
1 | 2023-10-01 10:00:00 | 2023-10-01 10:30:00 | 1800
2 | 2023-10-01 11:00:00 | 2023-10-01 12:15:00 | 4500
3 | 2023-10-01 13:00:00 | 2023-10-01 13:45:00 | 2700
(3 rows)
数据类型转化
dec(RATE,10,2) 函数转换*****************************************************************************
create table sales(id serial primary key,rate float);
insert into sales(rate) values(123.456),(789.123456),(456.789),(12.3),(0.987654); testdb_0903=> select id,rate,rate::numeric(10,2) as formatted_rate from sales;
id | rate | formatted_rate
----+---------------------+----------------
1 | 123.456000000000003 | 123.46
2 | 789.123456000000033 | 789.12
3 | 456.788999999999987 | 456.79
4 | 12.3000000000000007 | 12.30
5 | .987654000000000032 | .99
(5 rows) testdb_0903=>
testdb_0903=>
testdb_0903=> select id,rate,cast(rate as numeric(10,2)) as formatted_rate from sales;
id | rate | formatted_rate
----+---------------------+----------------
1 | 123.456000000000003 | 123.46
2 | 789.123456000000033 | 789.12
3 | 456.788999999999987 | 456.79
4 | 12.3000000000000007 | 12.30
5 | .987654000000000032 | .99
(5 rows) int(100.123) 函数转换*****************************************************************************
testdb_0903=> select cast(100.123 as int) as int_value from dual;
int_value
-----------
100
(1 row) testdb_0903=>
testdb_0903=> select 100.123::int as int_values from dual;
int_values
------------
100
(1 row) char(1) 函数转换*****************************************************************************
testdb_0903=> select cast(1 as char(1)) as char_value;
char_value
------------
1
(1 row) to_char(1) 函数转换*****************************************************************************
testdb_0903=> select to_char(current_timestamp,'HH12:MI:SS');
to_char
----------
10:14:46
(1 row) testdb_0903=> select to_char(125.8::real,'999D99');
to_char
---------
125.80
(1 row) to_date(1) 函数转换*****************************************************************************
testdb_0903=> select to_date('2024-09-30');
to_date
---------------------
2024-09-30 00:00:00
(1 row) testdb_0903=> select to_date('05 Dec 2000','DD Mon YYYY');
to_date
---------------------
2000-12-05 00:00:00
(1 row)
其它常用函数转换
locate('|','115|56',1) 函数转换*****************************************************************************
testdb_0903=> select instr('115|56','|',1) as position from dual;
position
----------
4
(1 row)
listagg(name,',') 函数转换*****************************************************************************
testdb_0903=> create table listagg_t1(a int,b text);
testdb_0903=> insert into listagg_t1 values(null,'a1'),(1,'b2'),(1,'c3'),(2,'d4'),(2,'e5'),(3,'f6');
testdb_0903=> select a,listagg(b,';') within group (order by b) from listagg_t1 group by 1;
a | listagg
---+---------
1 | b2;c3
2 | d4;e5
3 | f6
| a1
(4 rows)
nvl() 函数转换*****************************************************************************
testdb_0903=> select nvl(null,1);
nvl
-----
1
(1 row)
testdb_0903=>
testdb_0903=> select nvl('Hello world',1);
nvl
-------------
Hello world
(1 row)
trim() 函数转换*****************************************************************************
testdb_0903=> select trim(BOTH 'x' from 'xTommxx');
btrim
-------
Tomm
(1 row)
testdb_0903=> select trim(leading 'x' from 'xTommxx');
ltrim
--------
Tommxx
(1 row)
testdb_0903=> select trim(trailing 'x' from 'xTommxx');
rtrim
-------
xTomm
(1 row)
substr() 函数转换*****************************************************************************
testdb_0903=>
testdb_0903=> select substr('stringtest' from 4);
substr
---------
ingtest
(1 row)
testdb_0903=> select substr('stringtest',4);
substr
---------
ingtest
(1 row)
testdb_0903=>
testdb_0903=> select substr('teststring' from 5 for 2);
substr
--------
st
(1 row)
testdb_0903=> select substr('string',2,3);
substr
--------
tri
(1 row)
value() 函数转换*****************************************************************************
testdb_0903=> select coalesce(null,'hello');
coalesce
----------
hello
(1 row)
floor() 函数转换*****************************************************************************
testdb_0903=> select floor(-42.8);
floor
-------
-43
(1 row)
round() 函数转换*****************************************************************************
testdb_0903=> select round(42.4);
round
-------
42
(1 row)
coalesce() 函数转换*****************************************************************************
testdb_0903=> select coalesce(null,'hello');
coalesce
----------
hello
(1 row)
length() 函数转换*****************************************************************************
testdb_0903=> select length('abcd');
length
--------
4
(1 row)
testdb_0903=>
testdb_0903=> select length('汉字abc');
length
--------
5
(1 row)
posstr(exp1,exp2) 函数转换*****************************************************************************
testdb_0903=> select position('World' in 'Hello World') as pos;
pos
-----
7
(1 row)
replace() 函数转换*****************************************************************************
testdb_0903=> select replace('abcdefabcdef','cd','XXX');
replace
----------------
abXXXefabXXXef
(1 row)
testdb_0903=> select replace('abcdefabcdef','cd');
replace
----------
abefabef
(1 row)
GaussDB: db2->gaussdb 函数转换的更多相关文章
- DB2时间函数 实现 时间加减
时间加减:后边记得跟上时间类型如day.HOUR TIMESTAMP ( TIMESTAMP(DEF_TIME)+1 day)+18 HOUR DB2时间函数是我们最常见的函数之一,下面就为您介绍 ...
- SQLserver中用convert函数转换日期格式
SQLserver中用convert函数转换日期格式 2008-01-23 15:47 SQLserver中用convert函数转换日期格式2008-01-15 15:51SQLserver中用con ...
- DB 查询分析器 方便地创建DB2自定义函数
DB 查询分析器 方便地创建DB2自定义函数 马根峰 (广东联合电子服务股份有限公司, 广州 510300) 摘要 ...
- db2 OLAP函数使用
说起 DB2 在线分析处理,可以用很好很强大来形容.这项功能特别适用于各种统计查询,这些查询用通常的SQL很难实现,或者根本就无发实现.首先,我们从一个简单的例子开始,来一步一步揭开它神秘的面纱,请看 ...
- DB2 OLAP函数的使用
说起 DB2 在线分析处理,可以用很好很强大来形容.这项功能特别适用于各种统计查询,这些查询用通常的SQL很难实现,或者根本就无发实现.首先,我们从一个简单的例子开始,来一步一步揭开它神秘的面纱,请看 ...
- 如何将DB2的数据库转换到mySQL中?
经过几次尝试最终找到解决的办法,因为网上并没有详细说明,所以下面我将详细的步骤记录如下: 1. 大前提是你已经安装好DB2服务器和mySQL服务器,现在只是借用Navicat for MySQL这个软 ...
- SQLserver中用convert函数转换日期格式(1)
SQLserver中用convert函数转换日期格式2008-01-15 15:51SQLserver中用convert函数转换日期格式 SQL Server中文版的默认的日期字段datetime格式 ...
- Oracle的overlaps函数转换其他数据库语法
首先,来介绍一下Oracle的overlaps函数的用法: overlaps函数是用来判断两个时间段是否有重叠的 比如说计算 (a,b),(c,d) 就可以写成: select * from dual ...
- mysql 时间函数转换
1 NOW() //当前时间 2 SYSDATE() //当前时间 3 CURRENT_TIMESTAMP 4 以'YYYY-MM-DD HH:MM:SS'或YYYYMMDDHHMMSS格式返回当前的 ...
- DB2常用函数
1.char函数 char(current date,ISO)--转换成yyyy-mm-dd char(current date,USA)--转换成mm/dd/yyyy char(current ...
随机推荐
- 【转载】 NumPy之:数据类型对象dtype
原文地址: https://www.cnblogs.com/flydean/p/14720858.html ============================================== ...
- 如何在Ubuntu系统中进行系统级的代理设置
在Ubuntu系统中我们往往需要设置代理上网,比如在Ubuntu22.04中,通过图形化界面的设置方式如下: 这里假设代理服务器地址为: 192.168.1.103:1080 ============ ...
- avdmanager 返回了非零退出代码: 1。
最近做了一次系统还原,很多功能都出现了异常 重装了 Visual Studio 之后创建安卓仿真器的时候遇到问题,说"avdmanager 返回了非零退出代码: 1." 解决思路 ...
- STM32F3, STM32F4编程手册
1. Cortex-M4的内核设备 NVIC, Nested vectored interrupt controller SCB, System control block SysTick, The ...
- rk3568 | 瑞芯微平台GPIO引脚驱动编写
最近在玩瑞芯微平台的产品,移植了几个设备的驱动,遇到了一些问题,总结后发现大部分问题都出在了GPIO配置的问题上,写下本篇文章,用来分享一下调试的心得. 有喜欢瑞芯微的朋友,可以加我好友,拉你进群,一 ...
- FFmpeg开发笔记(四十八)从0开始搭建直播系统的开源软件架构
音视频技术的一个主要用途是直播,包括电视直播.电脑直播.手机直播等等,甚至在线课堂.在线问诊.安防监控等应用都属于直播系统的范畴.由于直播系统不仅涉及到音视频数据的编解码,还涉及到音视频数据的实时传 ...
- kubernetes重新初始化“[ERROR DirAvailable--var-lib-etcd]”
[root@master01 ~]# kubeadm init --config /root/kubeadm-config.yaml --upload-certs [init] Using Kuber ...
- 深度学习环境安装-conda-torch-Jupyter Notebook
conda的安装 为什么要安装这个,它是什么? 它是一个管理环境的,当我们跑项目的时候,往往这些项目所需要的pickets库和环境是不同的,这时候如果自己的电脑里面只有一个版本的库的话,就运行不了,比 ...
- vscode废掉了,跳转不到函数定义,无法自动补全,重装也没用的解决办法
1. 先卸载掉所有已安装的插件 2. 卸载vscode 3. 删除个人配置和插件配置,涉及两个文件夹 4. 需要安装C/C++组件,下载对应的vsix文件 下载地址: https://github.c ...
- C语言数据类型、变量的输入和输出、进制转换
scanf标准函数可以从键盘得到数字并记录到存储区里,为了使用这个标准函数需要包含stdio.h这个头文件 在scanf函数调用语句里应该使用存储区的地址表示存储区:双引号里使用占位符表示存储区的类型 ...