想要来自varchar2数据类型列的数字数据

时间:2012-05-09 05:14:07

标签: sql oracle

我有一个表Product,其varchar2数据类型为列名Value,在此列中值存储为

All,10:23,0.84522,1.245,10:54:68,
All,1:22:00,0.245,45:12:00 

等。

我们必须提取所有浮动值,如(0.84522,1.245,0.245)和以“:00”结尾的值(1:22:00,45:12:00)。

我有以下查询,但它似乎不起作用;它给了我除了字符之外的所有值。

select * from Product where Values BETWEEN to_char (0) and to_char (2);

2 个答案:

答案 0 :(得分:0)

我认为这会起作用

select *
FROM  Product 
WHERE  
(Value LIKE '%:00' AND Value<>  'ALL') AND (Value BETWEEN  to_NUMBER (0) and to_NUMBER (2))

答案 1 :(得分:0)

尝试此查询:

select *
  from (select distinct regexp_substr(t.value, '[^,]+', 1, level) phrase
          from Product t
        connect by regexp_substr(t.value, '[^,]+', 1, level) is not null) ph
 where regexp_like(ph.phrase, '(\d+\.\d+)|(.+:00)')

where子句中的正则表达式可能需要一些调整

它的作用是什么 -

  1. 分隔所有短语(内部查询)
  2. 仅选择符合条件的

  3. <强>更新
    如果您遇到表现,可以尝试不同的方法:

    create or replace type phrase_typ is object
    (
      phrase varchar2(100)
    )
    ;
    /
    create or replace type phrase_tab as table of phrase_typ;
    /
    create or replace function split_string(del in varchar2) return phrase_tab
      pipelined is
    
      phrase  varchar2(1000);
      str_t   varchar2(1000);
      v_del_i number;
    
      cursor c is with t as
        select value from product;
    
    begin
    
      for r in c loop
        str_t := r.value;
    
        while str_t is not null loop
    
          v_del_i := instr(str_t, del, 1, 1);
    
          if v_del_i = 0 then
            phrase := str_t;
            str_t  := '';
          else
            phrase := substr(str_t, 1, v_del_i - 1);
            str_t  := substr(str_t, v_del_i + 1);
          end if;
    
          if regexp_like(phrase, '(\d+\.\d+)|(.+:00)') then
            pipe row(phrase_typ(phrase));
          end if;
    
        end loop;
    
      end loop;
    
      return;
    end split_string;
    /
    

    现在您的查询应如下所示:

    select * from table(split_string(','))