oracle正则+with+质数求解

Oracle 正则表达式函数-REGEXP_SUBSTR 使用例子  5个参数

第一个是输入的字符串

第二个是正则表达式

第三个是标识从第几个字符开始正则表达式匹配。(默认为1)

第四个是标识第几个匹配组。(默认为1)

第五个是是取值范围:

i:大小写不敏感;

c:大小写敏感;

n:点号 . 不匹配换行符号;

m:多行模式;

x:扩展模式,忽略正则表达式中的空白字符。

 

全部测试数据

SQL> select * from test_reg_substr;

A

-----------------------------------

ABC123XYZ

ABC123XYZ456

<Name>Edward</Name>

 

检索中间的数字

SQL> SELECT

 2    REGEXP_SUBSTR(a,'[0-9]+')

 3  FROM

 4    test_reg_substr

 5  WHERE

 6    REGEXP_LIKE(a, '[0-9]+');

REGEXP_SUBSTR(A,'[0-9]+')

---------------------------------

123

123

 

检索中间的数字(从第一个字母开始匹配,找第2个匹配项目)

SQL> SELECT

 2    NVL(REGEXP_SUBSTR(a,'[0-9]+',1, 2), '-') AS a

 3  FROM

 4    test_reg_substr

 5  WHERE

 6    REGEXP_LIKE(a, '[0-9]+');

A

------------------------------------------------------

-

456

 

取得“字符集合”

SQL> SELECT

 2    REGEXP_SUBSTR(a, '\w+')

 3  FROM

 4    test_reg_substr

  5  WHERE

 6    REGEXP_LIKE(a, '\w+');

REGEXP_SUBSTR(A,'\W+')

-------------------------------

ABC123XYZ

ABC123XYZ456

Name

 

取得“字符集合”(从第一个字母开始匹配,找第2个匹配项目)

SQL> SELECT

 2    NVL(REGEXP_SUBSTR(a, '\w+',1, 2), '-') AS a

 3  FROM

 4    test_reg_substr

  5  WHERE

 6    REGEXP_LIKE(a, '\w+');

 A

---------------------------------------------------

 -

-

Edward
----------------------------------------------------应用pr_bubble_Sort('12,343,435,434')
create or replace procedure pr_bubble_Sort(
                   in_arry in VARCHAR2
                                           )
IS        ------定义一个表数据,建立虚拟表,避免实表问题
           type one_arr is table of number
           index by binary_integer;
           my_one_arr one_arr;     
           n_cur_index number(6):=0;

           n_inner_index number(6):=0;
           n_temp number(6);
BEGIN
                  -----在oracle中使用正则表达式
   for i in (select regexp_substr(in_arry,'[^,]+',1,rownum) my_date
   from dual
   connect by rownum<= length(in_arry)
                                 - length(replace(in_arry,',',''))+1)
   LOOP
        ----------------开始循环体
         n_cur_index := n_cur_index +1;
         my_one_arr(n_cur_index) := i.my_date;
   end loop;
  
   if in_arry  is not null then
         n_inner_index := n_cur_index;
        
         for j in 1..n_cur_index
         loop
             n_inner_index := n_inner_index -1;
             for k in 1..n_inner_index
             loop
                if my_one_arr(k) > my_one_arr(k+1) then
                   n_temp := my_one_arr(k);
                   my_one_arr(k) := my_one_arr(k+1);
                   my_one_arr(k+1) := n_temp;
                end if;
             end loop;
         end loop;
        
         for l in 1..n_cur_index
         loop
             dbms_output.put_line(my_one_arr(l));
         end loop;  
  
   end if ;

end;

 ----------------------------------------------------------------------排序
pl/sql 求选择排序
2011-03-04 09:11 17人阅读 评论(0) 收藏 举报
create or replace procedure pr_selection_Sort(in_arry in varchar2)
is
type one_arr is table of number index by binary_integer;
my_one_arr one_arr;
n_arr_index number(10):=0;
n_min number(10):=0;
n_temp number(10):=0;

n_inner_sel_ind number(10):=0;
begin
   for i in (select regexp_substr(in_arry,'[^,]+',1,rownum) my_data from dual
                    connect by rownum<= length(in_arry) - length(replace(in_arry,',',''))+1)
   loop
      n_arr_index := n_arr_index +1;
      my_one_arr(n_arr_index) := i.my_data;
   end loop;

   for j in 1..(n_arr_index-1)
   loop
      n_min := my_one_arr(j);
      n_inner_sel_ind := j;
      for k in (j+1)..n_arr_index
      loop
         if n_min>my_one_arr(k) then
           n_min := my_one_arr(k);
           n_inner_sel_ind := k;
         end if;
      end loop;
      n_temp := my_one_arr(j);
      my_one_arr(j) :=  my_one_arr(n_inner_sel_ind);
      my_one_arr(n_inner_sel_ind) := n_temp;

   end loop;
  
   for l in 1..n_arr_index
   loop
       dbms_output.put_line(my_one_arr(l));
   end loop;

end;

---------------------------------------------------------------------------求质数
用sql语句来完成求质数
2011-05-31 14:42 14人阅读 评论(0) 收藏 举报
With temp1 as(
Select rownum my_value_1 from dual
Connect by rownum<=50)
,temp2 as(
select rownum my_rn, my_value_1 from temp1
where ((mod(my_value_1,2)<>0 and mod(my_value_1,3)<>0)
or my_value_1 in(2,3)) and my_value_1<>1
)
,temp3 as(
select my_rn,my_value_1,wm_concat(my_value_1) over(order by my_rn) my_value_2 from temp2)
,temp4 as(
select rownum my_rn_1 from dual connect by rownum<=(select count(*) from temp2)
)
,temp5 as(
select my_rn,my_value_1,my_value_2, decode(mod(my_value_1,regexp_substr(my_value_2,'[^,]

+',1,my_rn_1)) ,0,1,0) total_score,my_rn_1 from temp3 a, temp4 b
where a.my_rn>b.my_rn_1)
select  my_rn,my_value_1,sum(total_score) from temp5
group by my_rn,my_value_1
having sum(total_score)=0
order by 1

---------------------------------------------------------------------------sql二分法
pl/sql 求二分法
2011-03-02 09:33 26人阅读 评论(0) 收藏 举报
create or replace procedure pr_half_find(in_num in number,in_arr in varchar2)
is

type arr_num is table of number index by binary_integer;
my_arr_num arr_num;
n_arr_index number(10):=0;

n_upper_bound number(10);
n_lower_bound number(10);
n_cur_index number(10);
begin

   for i in (select my_date from (
                    select regexp_substr(in_arr,'[^,]+',1,rownum) my_date from dual
                    connect by rownum<= length(in_arr) - length(replace(in_arr,',',''))+1
                    )
             order by to_number(my_date))
   loop
      n_arr_index := n_arr_index+1;
      my_arr_num(n_arr_index) := i.my_date;
      dbms_output.put_line(n_arr_index||'   '||my_arr_num(n_arr_index));

   end loop;
  
   n_upper_bound := 1;
   n_lower_bound := n_arr_index;
  
   if in_arr  is not null then
       while true
       loop
          n_cur_index := floor((n_upper_bound + n_lower_bound)/2);
          if my_arr_num(n_cur_index) = in_num then
             dbms_output.put_line('在第'||n_cur_index||'位');
             exit;
          end if;
         
          if n_upper_bound>=n_lower_bound then
             exit;
          end if;
          if my_arr_num(n_cur_index) > in_num then
             n_lower_bound := n_cur_index;
          else
             n_upper_bound := n_cur_index+1;
          end if;
         
       end loop;  
   end if;

end;
-----------------------------------------------求质数
create or replace procedure pr_prime(in_num in number)
is
type arr_prime is table of number INDEX BY BINARY_INTEGER;
my_arr_prime arr_prime;

n_cur_mult number(10):=1;
n_prime_1 number(10):=1;
n_prime_2 number(10):=2;
n_max number(10):= floor((in_num+1)/6);
n_arr_ind number(10):=1;
b_prime number(1):=0;
n_arr_max number(10):=2;
begin
   my_arr_prime(1):=2;
   my_arr_prime(2):=3;
   for i in 1..n_max loop
     n_prime_1 := i*6-1;
     n_prime_2 := i*6+1;
     n_cur_mult := ceil(sqrt(n_prime_2));
    

     for j in 1..2 loop
         if j =2 then
           n_prime_1:= n_prime_2;
         end if;
        
         if n_prime_1 > in_num then
           exit;
         end if;
         n_arr_ind:=1;
         b_prime :=0;
         while n_arr_ind <= n_arr_max and my_arr_prime(n_arr_ind)<= n_cur_mult loop
           if mod(n_prime_1, my_arr_prime(n_arr_ind)) =0 then
              b_prime :=1;
              exit;
           end if;
           n_arr_ind := n_arr_ind+1;
         end loop;

         if b_prime = 0 then
            n_arr_max := n_arr_max+1;
            my_arr_prime(n_arr_max) := n_prime_1;
           
         end if;
     end loop;


   end loop;
  
   --for i in 1..n_arr_max loop
    --dbms_output.put_line(my_arr_prime(i));
   --end loop;

end;

-----------------------------------------------------oracle中的sysdate做法
oracle 里sysdate里操作
2009-07-07 12:59 75人阅读 评论(0) 收藏 举报
最近在做查询时,我们如果用 sysdate -1 and sysdate来查询要对to_date('2009-7-6','yyyy-mm-dd')

and to_date('2009-7-7','yyyy-mm-dd')要慢上一些,如果要把sysdate转成日期的条件去,这样的查询速

度会更慢一些,所以我们pl/sql编程时,应该把sysdate先转成时间格式的字符串,如str_begin :=

to_char(sysdate-1,'yyyy-mm-dd')  str_end :=to_char(sysdate,'yyyy-mm-dd') 再用

select * from table where time to_date(str_begin,'yyyy-mm-dd') and to_date(str_end,'yyyy-mm-

dd')这样来操作也是比较理想的
--------------------------------------------------oracle中的with用法
oracle 里的with的用处
2009-07-05 13:30 38人阅读 评论(1) 收藏 举报
这几天对sql进行优化,发现在临时表对大量数据量很好帮助,特别是复杂的sql语句,特别是有多很多子查

询存在,如果我们使用select * from (select * from big_table1 where field =condition ) minus

select * from (select * from big_table2 where field =condition )这样子查询,在查询操作过程中,

会使数据查询会慢很多,当我们建立两个临时表分别存放select * from big_table1 where field

=condition 和select * from big_table2 where field =condition ,再用两个临时表来进行minus操作,

这样的操作速度会快很多,但这样一个问题就是要在写sql时要新有对应的临时表,这样对很多的不同的sql

语句说是很不适用,在oracle里提供了with这个好东西,让我们省了建立临时表,我们可以用with这样写

sql语句 with query1 as( select * from big_table1 where field =condition ) ,query2 as(select *

from big_table2 where field =condition) select * from query1 minus select * from query2来操作

,这样我们就可以省去了临时表,不过这样sql语句在我们使用ado来操作时,就会无效,这个是比较麻烦的

事,所以我们在写好的sql语句后select * from (with query1 as( select * from big_table1 where

field =condition ) ,query2 as(select * from big_table2 where field =condition) select * from

query1 minus select * from query2h),这样就可以解决了ado不识别with为前缀的sql语句了。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值