您的位置:首页 > 数据库 > Oracle

Oracle sql常用方法(持续更新)

2017-01-10 16:33 555 查看

1. 字符处理

1.去空格

--去前空格
select ltrim('   1 1   ') aa from dual;  --1 1
--去后空格
select rtrim('   1 1   ') aa from dual;  --   1 1
--去前后空格
select trim('   1 1   ') aa from dual;   --1 1
--去前后中空格
select replace('   aa  kk  ',' ','') abcd from dual;  --aakk


2.去回车符、换行符、空格符

最近碰到一条数据 值为:童勇\n 在java里面看不出来(显示:童勇 ),数据库里也看不出来(显示:童勇 ) 但在程序里面总不能出现想要的结果。



制表符 chr(9)

换行符 chr(10)

回车符 chr(13)

--去除换行
update zhzl_address t set t.add_administration_num=replace(t.add_administration_num,chr(10),'');
--去掉回车
update zhzl_address t set t.add_administration_num=replace(t.add_administration_num,chr(13),'');
--去掉空格
update zhzl_address t set t.add_administration_num=trim(t.add_administration_num);


**参考:oracle中去掉回车换行空格的方法详解

2. oracle表复制

复制表结构及其数据:

create table table_name_new as select * from table_name_old


只复制表结构:

create table table_name_new as select * from table_name_old where 1=2;
或者:
create table table_name_new like table_name_old


只复制表数据:

--如果两个表结构一样:
insert into table_name_new select * from table_name_old

--如果两个表结构不一样:
insert into table_name_new(column1,column2...) select column1,column2... from table_name_old


3. Oracle对字符的判断(不知原因)

每条数据可能有如下状态:



如表中现在的数据:



如果我们现在要取状态220_WAIT_FINANCIAL_PRE_APPROVE 到 390_HAS_IMPORT,sql如下:

select sbh.boe_header_id, sbh.boe_status
from sie.sie_boe_headers sbh
where sbh.boe_status >= '220'
and sbh.boe_status <= '390'
order by sbh.boe_header_id desc;


结果如下:



注意:

select sbh.boe_header_id, sbh.boe_status
from sie.sie_boe_headers sbh
where sbh.boe_status > '220'
and sbh.boe_status <= '390'
order by sbh.boe_header_id desc;


这个sql查询的结果跟上面的结果是一样的。可这里明明写的是 >220。

所以需要成如下才会出现我需要的结果(改为 >221)。

select sbh.boe_header_id, sbh.boe_status
from sie.sie_boe_headers sbh
where sbh.boe_status > '221'
and sbh.boe_status <= '390'
order by sbh.boe_header_id desc;


正确结果:



4. 字符串 splitstr

** splitstr函数不是oracle官方函数,是自定义的。参考:https://www.cnblogs.com/gnielee/archive/2009/09/09/1563154.html

select column_value as employee from table(splitstr( 'BGI5819,BGI11625,BGI11150,BGI11273,BGI7987',','))




日期特殊写法

select date '2017-01-24' from dual;




5. 行转列 wm_concat

sbh表中一个ID 对应 bl表是的多条数据

select sbh.boe_num, bl.boe_line_id
from SIE.SIE_BOE_HEADERS SBH, sie.sie_boe_lines bl
where sbh.boe_header_id = bl.boe_header_id
and sbh.boe_num = 'CRBC201506090057';




转列后

select sbh.boe_num, wm_concat(bl.boe_line_id)
from SIE.SIE_BOE_HEADERS SBH, sie.sie_boe_lines bl
where sbh.boe_header_id = bl.boe_header_id
and sbh.boe_num = 'CRBC201506090057'
group by sbh.boe_num;




start with connect by prior 递归查询用法

https://www.cnblogs.com/benbenduo/p/4588612.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  oracle sql