0

我有一个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);
4

2 回答 2

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. 仅选择符合您条件的那些

更新
如果您遇到性能问题,您可以尝试不同的方法:

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(','))
于 2012-05-09T06:00:03.153 回答
0

我认为这会奏效

select *
FROM  Product 
WHERE  
(Value LIKE '%:00' AND Value<>  'ALL') AND (Value BETWEEN  to_NUMBER (0) and to_NUMBER (2))
于 2012-05-09T05:29:59.743 回答