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.htmlselect 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语句(持续更新)
- Oracle常用SQL汇总(持续更新)
- 【数据库】oracle常用sql总结(持续更新中)
- 数据库-oracle常用SQL语句整理(持续更新中)
- oms项目常用执行sql方法(增删改查)持续更新
- Oracle--sqlplus常用命令,持续更新...
- ORACLE常用查询命令,持续更新......
- oracle常用sql语句(不断更新中)
- ORACLE常用函数汇总(持续更新中....)
- oracle常用命令--SQL/Plus(不断更新)
- oracle DBA常用操作语句 持续更新。。。
- SQL oracle 用rownum方法查询最近更新的n条数据
- Java常用方法总结(持续更新中)
- 常用JS方法集合(原创-持续更新)
- oracle SQL进阶篇(持续更新中)
- Oracle SQL开发笔记(持续更新中...)
- jsf几个常用取值方法和技巧总结(持续更新)
- MySql常用命令及sql 技巧 (持续更新)
- oracle 常用SQL(不断更新)
- oracle常用命令(日常整理,持续更新)