Oracle database is migrated to Aurora (PostgreSQL) database, SQL correction

Realize business

In order to welcome events such as Double 11, increase the server capacity and upgrade the original database. User data is still stored on the original server, and other data is stored on Google Cloud.

Empty string

Oracle SQL
select * from table name where field is not null
PostgreSQL
select * from table name where field is not null and field<>''

substr

The first case
Oracle SQL
select * from table name p where substr(p. field, 0, 4) = xx
PostgreSQL
select * from table name p where substr(p. field, 1, 4) = xx
The second case
Oracle SQL
select * from table name where substr(field, -3) = xx
PostgreSQL
select * from table name where substr(field,'...$') = xx

Rownum

第一种情况
Oracle SQL
select ‘test001’ as a from dual where rownum=1
PostgreSQL
select a from(select ‘test001’ as a,row_number() over () as rownum) r where rownum=1
第二种情况
Oracle SQL
select ‘test001’ as a from dual where rownum= ‘xx’
PostgreSQL
select a from(select ‘test001’ as a,row_number() over () as rownum) r where rownum <= to_number(#{xx},‘9999’)

sysdate

Oracle SQL
insert table name (field) values(sysdate)
PostgreSQL
insert table name (field) values(current_timestamp)

Subquery alias

Oracle SQL
select * from (select ‘test001’ as a from dual) where a =‘test001’;
PostgreSQL
select * from(select ‘test001’ as a) r where a=‘test001’;

to_date

The first case
Oracle SQL
select to_date('20200101') from dual;
PostgreSQL
select to_date('20200101','yyyyMMdd'); the
second case
Oracle SQL
select to_date('20200101'||'23:59:59' ,'YYYYMMDD
HH24:MI:SS') from dual;
PostgreSQL
select to_timestamp('20200101'||'23:59:59','YYYYMMDD HH24:MI:SS')

Month calculation add_months

Oracle SQL
select add_months(to_date(‘20200101’,‘yyyyMMdd’),‘MM’) from dual;
PostgreSQL
select to_date(‘20200101’,‘yyyyMMdd’)-interval ‘24 month’;

trunc

Oracle SQL
select trunc(to_date(‘20200101’,‘yyyyMMdd’),‘MM’) from dual;
PostgreSQL
select date_trunc(‘month’,to_date(‘20200101’,‘yyyyMMdd’));

between…to…

Oracle SQL
select * from 表名 where 字段 between to_number(‘20200901’) and ‘20200930’;
PostgreSQL
select * from 表名 where to_number(字段,‘99999999’) between to_number(‘20200901’,‘99999999’) and to_number(‘20200930’,‘99999999’);

to_char

Oracle SQL
select to_char(#{regTms},‘YYYYMMDD’) from dual;
※#{regTms}=2020-08-28 00:00:00.0

Guess you like

Origin blog.csdn.net/qq_41520636/article/details/110239511