首页 > 数据库 >GaussDB: db2->gaussdb 函数转换

GaussDB: db2->gaussdb 函数转换

时间:2024-10-22 11:32:21浏览次数:1  
标签:00 GaussDB testdb 0903 2023 date db2 gaussdb select

一、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)

 

标签:00,GaussDB,testdb,0903,2023,date,db2,gaussdb,select
From: https://www.cnblogs.com/avasteven/p/18492245

相关文章

  • 一图为你揭秘云数据库GaussDB管理平台亮点
    云数据库GaussDB管理平台(TPOPS)是一款即开即用、稳定可靠、管理便捷的数据库运维管理平台。通过该平台,用户可以快速部署安装GauSSDB,实现智能化运维,大幅度提升运维和管理效率。一图带你揭秘云数据库GaussDB管理平台亮点。 华为开发者空间,汇聚鸿蒙、昇腾、鲲鹏、GaussDB、欧拉等......
  • gaussdb 基础管理 数据库 表 用户 模式 权限 存储过程
    数据库database#创建数据库,指定字符集UTF8,缺省情况下新数据库将通过复制标准系统数据库template0来创建,且仅支持使用template0来创建。CREATEDATABASEdevdbENCODING'UTF8'template=template0;CREATEDATABASEtestdb;标识符的命名需要遵守如下规范:*标识符需要为......
  • Navicat Premium 17.1.3版本发布,原生支持华为云数据仓库GaussDB(DWS)
     近日,Navicat发布了一项重要更新,原生支持华为云数据仓库GaussDB(DWS)。这是Navicat支持的国内首个数据仓库产品,将为广大GaussDB(DWS)用户带来更多的便利。Navicat无缝对接GaussDB(DWS),让数据仓库的构建、查询、优化及维护变得更加直观和易于管理,助力企业轻松驾驭大数据时代的......
  • db2存储过程
    在DB2数据库中调用存储过程,可以使用`CALL`语句。下面是调用存储过程的基本语法: ```sqlCALLprocedure_name([parameter1,parameter2,...]);``` 这里的`procedure_name`是你要调用的存储过程的名字,而`[parameter1,parameter2,...]`是你传递给存储过程的参数列表(如果......
  • 技术解读GaussDB (for MySQL)流控机制
    本文分享自华为云社区《【华为云MySQL技术专栏】GaussDB(forMySQL)流控技术解读》,作者:GaussDB数据库。本文主要介绍GaussDB(forMySQL)在不同服务层次上是如何实现过载保护的,具体包括反馈式和主动平滑流控两种机制。1.背景介绍GaussDB(forMySQL)是存储计算分离架构的云......
  • 对GaussDB数据库和数据管理的简单介绍
    一、前言数据库与数据管理有着密切的关系,两者共同构成了一个完整的、可扩展的数据库管理系统。数据库是用于存储数据的系统,为数据提供了安全、可靠、可扩展和可管理的存储环境。随着信息技术的飞速发展,数据已经成为企业的核心资产之一。在这个数据驱动的时代,数据管理成为了企业......
  • 如何设计好分布式数据库,这个策略很重要(GaussDB)
    ​数据库是应用和计算机的核心组成,试想,如果没有数据库,就像人的大脑没有了记忆一样,信息也得不到共享,那么,对开发者来说,如何设计一款高效易用的数据库至关重要。GaussDB是企业级分布式数据库,具备分布式强一致、有效降低容灾成本、支持PB级海量数据、智能诊断等优点,是当下炙手可热的......
  • GaussDB数据库特性-物化视图简介
    一、前言随着企业数据量的不断增长和业务需求的复杂性增加,选择一个高效、可靠且智能的数据存储和管理解决方案变得越来越重要。GaussDB是一种先进的关系型数据库管理系统,为企业提供了强大的数据处理能力,其物化视图(MaterializedViews)功能在数据查询和管理方面具有重要作用。本文......
  • GaussDB技术解读系列:如何迁移到GaussDB_
    ​迁移是数据库选型过程中客户最为关心的话题之一,经过大量的沟通调研,我们总结了三个客户在数据库迁移方面的主要期望:迁移不影响业务运行(安心),迁移不能丢数据(放心),迁移实施起来简单(省心)。本文从客户视角的三个疑问出发,一起了解华为云GaussDB数据库的迁移解决方案具有哪些核心技术,如......
  • GaussDB数据库SQL系列-LOCK TABLE
    一、前言GaussDB是一款高性能、高可用的分布式数据库,广泛应用于各类行业和场景。在GaussDB中,锁是实现并发控制的关键机制之一,用于协调多个事务之间的数据访问,确保数据的一致性和完整性。本文将围绕GaussDB数据库的LOCKTABLE做一简单介绍。二、GaussDB数据库的锁GaussDB提供了......