ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 选取以逗号分隔的字段

选取以逗号分隔的字段

原创 Linux操作系统 作者:wouwouwou 时间:2010-12-18 19:20:01 0 删除 编辑
SQL> select * from ss;
DM         NAME
---------- ----------
170        a
10170      b
170,160    c
150,170    d
17010      e
选取DM为“170”的记录,即第1,3,4记录,允许逗号发隔,不要选到"10170”、"17010”。
最佳答案是论坛朋友给的:
SQL> select * from ss where instr(','||dm||',',',170,')>0;
DM         NAME
---------- ----------
170        a
170,160    c
150,170    d
自己造函数也可达到该目的:
 创建函数:
create       or       replace       function       is_contain(       c1       varchar2,       c2       varchar2)  
      return       number       as  
          st number := 1;
   ed number;  
      begin  
          if       c1       is       null       or       c2       is       null       then  
              return       0;  
          end       if;
          loop  
              ed := instr(c1, ',', st);
  if ed = 0 then
   if substr(c1, st) = c2 then
    return 1;
   else
    return 0;
   end if;
  end if;
              if       c2=substr(c1,st,ed-1) then  
                  return       1;  
              end       if;  
              st := ed + 1;
          end       loop;  
          return       0;  
      end;  
      /
执行结果:
SQL> select * from ss;
DM         NAME
---------- ----------
170        a
10170      b
170,160    c
150,170    d
17010      e
SQL> select * from ss where is_contain(dm,'170')=1;
DM         NAME
---------- ----------
170        a
170,160    c
150,170    d
    
   

 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/24825020/viewspace-682090/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2010-12-09

  • 博文量
    6
  • 访问量
    12283