oracle常用语句

1.去掉字段空格(可将所有的空格都去掉):replace(t.service_type,' ','');

2.时间:to_date('2018/1/1', 'yyyy-mm-dd'); 

select to_date(to_char(sysdate,'yyyy-mm-dd')|| ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss') from dual; ---保存时,使用某天的23::59:59分数据

3.取某个字段值中;的第一个:substr(temp.email, 0, instr(temp.email,';');

注意:若某个字段的值是空,则不能使用null != 'TYPE',

需要转换一下,不然会查询不到值:nvl(UPPER(A.sex),'N') <> 'F'

4.多列转行或是树结构:sys_connect_by_path(t.dz, '#')

如:

4.某个字段如下:a,b,c,想要分开显示为三条数据:(后面的只是取的1,2的值而已。若是知道会有多少个,的值,则需要union多次,然后再去掉空值即可)

select regexp_substr('a,b,c,d,e,','[^,]+',1,rownum)   
       from dual connect by rownum<=length(regexp_replace('a,b,c,', '[^,]', null));

---- 或是:

select regexp_substr(service_type, '[^,]+', 1, 1) str,
                         a.supplier_number
                    from  A a
UNION all
select regexp_substr(service_type, '[^,]+', 1, 2) str,
                         a.supplier_number
                    from  A a    

---- 或是:

select t.supplier_number,
       substr(t.service_type, 0, instr(t.service_type, ',') - 1) as prefix
       /*,substr(t.service_type,
              instr(t.service_type, ',') + 1,
              length(t.service_type)) as suffix*/
  from test1 t
  union all
select t.supplier_number,
       /*substr(t.service_type, 0, instr(t.service_type, ',') - 1) as prefix,*/
       substr(t.service_type,
              instr(t.service_type, ',') + 1,
              length(t.service_type)) as suffix
  from test1 t where t.service_type is not null order by supplier_number,prefix asc

数据:

结果:

5.存储过程:入门学习(https://www.cnblogs.com/dc-earl/articles/9260111.html,https://www.cnblogs.com/dc-earl/articles/9265144.html)

基本结构:

create or replace procedure 名称(name in varchar,count out int) --- 若没有参数时,可不用带上(),in输入参数,out输出参数

as /is ---其中视图只能用as,游标只能用is

---声明变量,省份

sf varchar(50);---varchar必须指定长度

begin

```````

sf:='GX';---变量赋值

dbms_output.put_line('name='||name||',age='||age);---控制台打印,||连接字符串

end 名称;

----- 异常:

执行:https://www.cnblogs.com/sdd53home/p/5169046.html

有参数的调用:

declare

 name varchar(10);
 age int;
begin
  myDemo05(name=>name,age=>10);
  dbms_output.put_line('name='||name);
end;

 6.oracle中的utl_http:可以捕捉网站页面的内容或是调用一个url的接口完成某项功能

https://blog.csdn.net/rznice/article/details/72625680

7.CHR() --将ASCII码转换为字符;ASCII() --将字符转换为ASCII码

https://blog.csdn.net/wangnan537/article/details/17676037

 8.在排序时,null默认是最大值,

order by P.parental_ib ,P.creation_date desc(parental_ib字段使用的是升序,creation_date降序)

order by P.parental_ib desc ,P.creation_date desc(parental_ib字段使用的是降序切Null在前面,creation_date降序)

order by P.parental_ib desc nulls last,P.creation_date desc (添加了nulls last会将Null放在最后)

9.有的时候想要对某些数据进行组函数的计算时,但是其他的不想使用组函数,可以使用如下:

sum(l.after_tax_payment_amount)over(partition  by  h.INVOICE_NUMBER order by  h.INVOICE_NUMBER )after_tax_payment_amount,

这样,就可以按照分组,将数据计算了

posted on 2019-01-08 08:47  活在当下L  阅读(203)  评论(0编辑  收藏  举报

导航